如何在gridview中右侧移动自动生成删除按钮

时间:2013-02-12 07:15:15

标签: c# asp.net gridview

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

Delete | Name | ContactNo | EmailID | CreateDate |

删除列是自动生成的,我想将其移到右侧,如

 | Name | ContactNo | EmailID | CreateDate | Delete

我该怎么办?

3 个答案:

答案 0 :(得分:6)

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

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

<asp:CommandField ShowDeleteButton="True" />

或者,您必须创建一个用于删除的按钮模板列。

答案 1 :(得分:3)

  1. 首先,您无法使用自动生成的删除按钮执行此操作,因此您必须设置AutoGenerateDeleteButton="false"
  2. 为删除按钮创建CommandField,并将其放在所有其他列的下方,以使其显示在右侧:
  3. <强> 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; }
    }
    

答案 2 :(得分:0)

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());
        }