简体   繁体   中英

Grid View Dynamically Generated Column

I have Grid View which is dynamically binded with datatables.

I had to add in the last column a command field AAddOn

When trying like below method..AAddOn is displayed at first..

How can we display the command field at the last..

  <asp:GridView ID="AGridView" runat="server" AutoGenerateColumns="true"  style="table-layout:fixed;" Width="2000px"   RowStyle-HorizontalAlign="Left">
            <EmptyDataTemplate>
               &nbsp;
           </EmptyDataTemplate>
            <asp:CommandField ShowEditButton="True" ItemStyle-Width="80px" EditText="Edit Add On">
             <ItemStyle Font-Bold="true" Font-Size="Small" />
              <HeaderStyle CssClass="AAddOn" />
             </asp:CommandField>
      </asp:GridView>

For gridview, defined columns always render first then the auto generated columns render on the right side of it. To move auto generated columns to left side, you need RowCreated event. There you can manipulate the order of columns as required. You can use below code.

protected void AGridView_RowCreated(object sender, GridViewRowEventArgs e){
        List<TableCell> cellColumns = new List<TableCell>();
        foreach (DataControlField column in GridView1.Columns)
        {
            TableCell cell = e.Row.Cells[0];
            e.Row.Cells.Remove(cell);
            cellColumns.Add(cell);
        }

        e.Row.Cells.AddRange(cellColumns .ToArray());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM