views:

2968

answers:

4

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?

A: 

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.

Martin
+1  A: 

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.

brendan
+1  A: 

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
madcolor
A: 

If somebody still need answer: just use e.Row.Cells.Count from RowDataBound.

Quiz