Any way to manipulate columns in a GridView with AutoGenerateColumns = true?

There seems to be no way to manipulate Gridview columns if AutoGenerateColumns = true. Here is my scenario:

I have a generic GridView that displays the results of various LINQ queries depending on what the user selects. I like the fact that AutoGenerateColumns works as it should, and I do not need to specify all the columns of BoundField, TemplateField, etc.

In addition, I also programmatically add other columns as needed. Tables that are added programmatically are displayed to the left of the auto-generated columns. What if I want to move them to the right?

GridView.Columns.Count only counts those that are programmed, not auto-generated, so I cannot reorder the columns I need. I can hook up the RowDataBound event and β€œhide” something if necessary, but I cannot reorder it.

Should I just discard AutoGeneratedColumns = true and lay them out with BoundFields for each query? Is there anything I can do?

+7
gridview
source share
5 answers

You can manipulate things with data binding as follows:

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 will need to create your own title, but it is very convenient.

+3
source share

Brendan's answer reminded me that I was lying around. Good for formatting.

GridView ...

 <asp:GridView .... OnRowDataBound="myGridView_RowDataBound"> 

Code for ...

 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 
+1
source share

I do not think that it is possible to control auto-generated columns, at least with the current GridView.

By creating a new control that inherits from the GridView, you may have a bit more control over how the columns are created, but I'm not sure how feasible it is (maybe worth exploring)

In 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 displayed as a column in the GridView control in the order in which the fields appear in the data source. This option provides a convenient way to display each field in the source data; however, you are limited to controlling how the automatically generated column field behaves.

Automatically generated linked column fields are not added to collection columns.

Instead of managing the GridView to automatically generate column fields, you can manually define the column fields to set the auto-generator property to false, and then create a custom column collection. In addition to the related column fields, you can also display the button column field, check the field column field, command field, hyperlink column field, image field or column field based on your own custom template. See the columns for more information.

0
source share

If anyone else needs an answer: just use e.Row.Cells.Count from RowDataBound .

0
source share

Based on the accepted answer, a dictionary can be created to map column names to columns in the RowDataBound event to allow the use of header names. Column sharing is also 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; } 
0
source share

All Articles