简体   繁体   中英

Change the order of the commandField in gridview?

I have a gridview which is totatly bounded by programatically .And i added a command field for selection .But it shows up on the first column .Bu i want to change its order to the last column. I tried to use Displayindex , but that column doesn't involve a headerrow.

Here is my gridview looks like

         UserID   UserName  UserAddress 
    -------------------------------------------------
  Select   1      testname  testaddress

I just want to pass Select to the last column

         UserID   UserName  UserAddress 
    -------------------------------------------------
          1      testname  testaddress   Select 


 <asp:GridView ID="GridForUnits" runat="server" CellPadding="4" ForeColor="#333333"
                        GridLines="None" Style="text-align: left" Width="851px" OnRowDataBound="GridForUnits_RowDataBound"
                        OnSelectedIndexChanged="GridForUnits_SelectedIndexChanged1">
                        <AlternatingRowStyle BackColor="White" />
                        <Columns>
                            <asp:CommandField ShowSelectButton="True" />
                        </Columns>
                        <EditRowStyle BackColor="#7C6F57" />
                        <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                        <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
                        <RowStyle BackColor="#E3EAEB" />
                        <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
                        <SortedAscendingCellStyle BackColor="#F8FAFA" />
                        <SortedAscendingHeaderStyle BackColor="#246B61" />
                        <SortedDescendingCellStyle BackColor="#D4DFE1" />
                        <SortedDescendingHeaderStyle BackColor="#15524A" />
                    </asp:GridView>

Any Help Appreciate.

Thanks

I would recomend to not auto generate the columns, fill them manually, and then change the visible index of all of them , setting the select button the last one.

More explanation here

Other solution: move the TableCells around in the RowDataBound event:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        GridViewRow row = e.Row;


        List<TableCell> cells = new List<TableCell>();


        foreach (DataControlField column in GridView1.Columns)
        {
            // Retrieve first cell
            TableCell cell = row.Cells[0];


            // Remove cell
            row.Cells.Remove(cell);


            // Add cell to list
            cells.Add(cell);
        }


        // Add cells
        row.Cells.AddRange(cells.ToArray());
    }

Link to source

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