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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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.

Method 3

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

Method 4

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

Method 5

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;
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x