views:

546

answers:

3

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

+3  A: 

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>
Scott Ivey
Thanks. It would be the HyperLinkField not the regular hyperlink controlwhich gives an error.
Tony_Henrich
+1  A: 

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

A: 

Thank you very much this answer is working for me, and can you help me for nested gridview in the same scenario.

Rajan