views:

265

answers:

1

I have a datatable being built in code which can dynamically have any given number of columns. I have to group the rows within the Gridview by adding a new TableRow based on the first column. This requirement means I can't use the AutoGenerateColumns. I've create a table within an ItemTemplate which I'm using to bind the rows/cells against the columns in the ItemDatabound event. This works fine and does what I need to do. The problem I have is that I can't bind my header to the name of the columns. When I check the RowType is a Header on the ItemDatabound I don't have an e.Row.DataItem available because this starts on the 1st data row. As a result I can't check what the ColumnName is with which I want to print to the header cell of that column.

Here is the code I'm using on the DataRow (code is in Delphi.net but you should be able to get the gyst if you use c# or VB too):

if (e.Row.RowType = DataControlRowType.DataRow) then begin

    if panlViewAllResults.Visible then begin
      for i := 0 to lDataRow.DataView.Table.Columns.Count - 1 do begin
        if Assigned(lDataRow.Item[i]) then begin
          lCell := TableCell.Create;
          lCell.Text := lDataRow.Item[i].ToString;
          lRow1.Controls.Add(lCell);
        end;
      end;

      lTable.Controls.Add(lRow1);
    end;   
end;

I thought I could do something like this for the header. But apparantly not:

  if (e.Row.RowType = DataControlRowType.Header) then begin
    for i := 0 to lDataRow.DataView.Table.Columns.Count - 1 do begin
            if Assigned(lDataRow.Item[i]) then begin
              lCell := TableCell.Create;
              lCell.Text := lDataRow.DataView.Table.Columns[i].ColumnName;
            end;
    end;
  end;

Hoping someone can throw some light on this. I'm a little stuck to say the least.

TIA Lloyd

+1  A: 

You could do this logic in the event OnDataBound. You'll need to loop through all your rows and you will have access to all the data you need at this point.

joerage