Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 the results of various different LINQ queries depending upon what the user selects. I like the fact that the AutoGenerateColumns works like it should and I don't have to specify all the BoundField, TemplateField columns, etc...

On top of that, I'm also programatically adding other columns as needed. The columns that are programatically added are rendered to the left of the autogenerated columns. What if I wanted to move them to the right?

GridView.Columns.Count only counts those that are programmed, not autogenerated, so I can't rearrange the columns I want around. I can hook the RowDataBound event and "hide" something if necessary, but I can't rearrange.

Do I just have to give up AutoGeneratedColumns=true, and lay them out with BoundFields for each query? Is there anything I can do?

like image 242
Robert4Real Avatar asked Mar 26 '09 16:03

Robert4Real


People also ask

What is the use of AutoGenerateColumns in GridView?

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.

What is AutoGenerateColumns?

AutoGenerateColumns. Gets or sets a value indicating whether bound fields are automatically created for each field in the data source.

How do you define the fields in a GridView control explain?

The GridView control displays the values of a data source in a table. Each column represents a field, while each row represents a record. The GridView control supports the following features: Binding to data source controls, such as SqlDataSource.


2 Answers

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.

like image 97
brendan Avatar answered Oct 26 '22 18:10

brendan


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.

like image 28
Martin Avatar answered Oct 26 '22 19:10

Martin