Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert a column in a gridview after the auto generated columns - ASP.NET

This is my gridview:

<asp:GridView ID="gridview" runat="server" AutoGenerateColumns="true">
    <Columns>
        <asp:TemplateField HeaderText="TestColumn">
            <ItemTemplate>
                <asp:LinkButton ID="lkbtn" runat="server" Text="Edit"
                    CommandName="Update" CausesValidation="False" ToolTip="Edit" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

The TestColumn ends up being the first column, but i want it after the auto generated ones.

like image 766
WoF_Angel Avatar asked Aug 02 '11 13:08

WoF_Angel


1 Answers

In the RowDataBound event handler, you can move the TemplateField cell from the first column to the end of the row:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    TableCell cell = e.Row.Cells[0];
    e.Row.Cells.RemoveAt(0);
    e.Row.Cells.Add(cell);
}
like image 52
ConnorsFan Avatar answered Sep 28 '22 07:09

ConnorsFan