如何在网格视图的右侧移动自动生成删除按钮

本文关键字:移动 自动生成 删除 按钮 网格 视图 | 更新日期: 2023-09-27 17:58:42

我有一个具有以下列的网格视图

Delete | Name | ContactNo | EmailID | CreateDate |

删除列是自动生成的,我想把它像一样移到右边

 | Name | ContactNo | EmailID | CreateDate | Delete

我该怎么做???

如何在网格视图的右侧移动自动生成删除按钮

AutoGenerateDeleteButton在设计时将不可见,因为VS将在运行时自动添加它,据我所知,它将自动添加到网格的最左侧,基本上是默认情况。

您可以尝试在设计视图中添加以下命令字段。

<asp:CommandField ShowDeleteButton="True" />

否则,您必须创建一个按钮模板列进行删除。

  1. 首先,您不能使用自动生成的删除按钮来执行此操作,因此必须设置AutoGenerateDeleteButton="false"
  2. 为删除按钮创建一个CommandField,并将其放置在所有其他列的下方,这样它就会显示在右侧:

ASPX:

<asp:GridView ID="gvEmployees" runat="server" AutoGenerateDeleteButton="false" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Id" />
        <asp:BoundField DataField="Name" />
        <asp:CommandField DeleteText="Delete" ShowDeleteButton="true" />
    </Columns>
</asp:GridView>

代码隐藏:

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            var employees = new List<Employee>{new Employee{Id="1",Name="Employee 1"}};
            gvEmployees.DataSource = employees;
            gvEmployees.DataBind();
        }
    }
}
public class Employee
{
    public string Id { get; set; }
    public string Name { get; set; }
}
create a RowCreated Event on your grid... easy as that (this will swap the row to end,, no need to turn off auto generate... 100% worked for me)
 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            GridViewRow row = e.Row;
            // Intitialize TableCell list
            List<TableCell> columns = new List<TableCell>();
            foreach (DataControlField column in GridView1.Columns)
            {
                //Get the first Cell /Column
                TableCell cell = row.Cells[0];
                // Then Remove it after
                row.Cells.Remove(cell);
                //And Add it to the List Collections
                columns.Add(cell);
            }
            // Add cells
            row.Cells.AddRange(columns.ToArray());
        }