title: Programatically binding a header in a Gridview
body: <p>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.</p>
  
  <p>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):</p>
  
  <pre><code>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;
  </code></pre>
  
  <p>I thought I could do something like this for the header. But apparantly not:</p>
  
  <pre><code>  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;
  </code></pre>
  
  <p>Hoping someone can throw some light on this. I'm a little stuck to say the least.</p>
  
  <p>TIA
  Lloyd</p>