在gridview中更改commandField的顺序?

时间:2013-05-22 15:32:35

标签: c# asp.net

我有一个以编程方式完全限制的gridview。我添加了一个命令字段供选择。但它显示在第一列.Bu我想将其顺序更改为最后一列。我尝试使用Displayindex,但该列不涉及标题。

这是我的gridview看起来像

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

我只想将Select传递给最后一列

         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>

任何帮助感谢。

由于

1 个答案:

答案 0 :(得分:0)

我建议不要自动生成列,手动填充它们,然后更改所有列的可见索引,将选择按钮设置为最后一个。

More explanation here

其他解决方案:在RowDataBound事件中移动TableCells:

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