It seems like there\'s no way to manipulate the columns of a Gridview if AutoGenerateColumns = true. Here\'s my scenario:
I\'ve got a generic GridView that displays
You can manipulate things on data bound like this:
Private Sub MyGrid_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles Me.RowDataBound
If Me.AutoGenerateColumns = True Then
If e.Row.RowType = DataControlRowType.DataRow Then
e.row.cells.add(some code here to add your special column)
End If
End If
End Sub
You'd have to create your own header to but it's very doable.
I don't think that it's possible to control the autogenerated columns, at least with the current GridView.
By Creating a new control that inherits from the GridView, you might have a bit more control of the way the columns are created, but I'm not shure if it is doable (might still be worth to research)
From the MSDN Documentation:
When the AutoGenerateColumns property is set to true, an AutoGeneratedField object is automatically created for each field in the data source. Each field is then displayed as a column in the GridView control in the order that the fields appear in the data source. This option provides a convenient way to display every field in the data source; however, you have limited control of how an automatically generated column field is displayed or behaves.
Automatically generated bound column fields are not added to the Columns collection.
Instead of letting the GridView control automatically generate the column fields, you can manually define the column fields by setting the AutoGenerateColumns property to false and then creating a custom Columns collection. In addition to bound column fields, you can also display a button column field, a check box column field, a command field, a hyperlink column field, an image field, or a column field based on your own custom-defined template. For more information, see Columns.
Brendan's answer reminded me I had this lying around.. Good for formatting.
GridView...
<asp:GridView .... OnRowDataBound="myGridView_RowDataBound">
Code Behind...
Sub myGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
' Display the data in italics.
e.Row.Cells(1).Text = "<i>" & e.Row.Cells(1).Text & "</i>"
End If
End Sub
Building on the accepted answer, a dictionary can be created to map from column names to column indicies in the RowDataBound event to allow use of header names. Also a column swap is shown.
Dictionary<string, int> _columnIndiciesForAbcGridView = null;
protected void detailsReportGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (_columnIndiciesForAbcGridView == null)
{
int index = 0;
_columnIndiciesForAbcGridView = ((Table)((GridView)sender).Controls[0]).Rows[0].Cells
.Cast<TableCell>()
.ToDictionary(c => c.Text, c => index++);
}
// Add a column, this shifts the _columnIndiciesForAbcGridView though.
TableCell cell = new TableCell();
cell.Text = "new Column";
e.Row.Cells.AddAt(2, cell);
// Swap 0 and 1
int c0 = _columnIndiciesForAbcGridView["ConfigId"];
int c1 = _columnIndiciesForAbcGridView["CreatedUtc"];
string text = e.Row.Cells[c0].Text;
e.Row.Cells[c0].Text = e.Row.Cells[c1].Text;
e.Row.Cells[c1].Text = text;
}
If somebody still need answer: just use e.Row.Cells.Count from RowDataBound.