Tony_Henrich
Tony_Henrich

Reputation: 44085

How to add new column to asp.net gridview where columns are autogenerated?

How to add new hyperlink column to an asp.net gridview where columns are autogenerated? The columns are not predefined in the gridview.

Upvotes: 1

Views: 6394

Answers (2)

James in Indy
James in Indy

Reputation:

I find that autogenerated columns appear to the RIGHT. If you want them to be on the left, you have to add code to the RowCreated event which deletes and re-adds all columns, like so:

  protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
  {
        GridViewRow row = e.Row;
        List<TableCell> columns = new List<TableCell>();

        foreach (DataControlField column in GridView1.Columns)
        {
            TableCell cell = row.Cells[0];
            row.Cells.Remove(cell);
            columns.Add(cell);
        }

        row.Cells.AddRange(columns.ToArray());
    }

Found article here: http://geekswithblogs.net/dotNETvinz/archive/2009/06/03/move--autogenerate-columns-at-leftmost-part-of-the-gridview.aspx

Upvotes: 2

Scott Ivey
Scott Ivey

Reputation: 41558

Just add your column definition the section of the gridview. Your auto-generated columns should show up to the left of this one.

<asp:gridview AutoGenerateColumns="true" ... >
    <columns>
        <asp:hyperlink ... />
    </columns>
</asp:gridview>

Upvotes: 4

Related Questions