我有一个具有以下列的gridview
Delete | Name | ContactNo | EmailID | CreateDate |
删除列是自动生成的,我想将它移动到右侧
| Name | ContactNo | EmailID | CreateDate | Delete
我怎样才能做到这一点???
AutoGenerateDeleteButton在设计时将不可见,因为 VS 会在运行时自动添加它,据我所知,这将自动添加到网格的最左侧,基本上这是默认设置。
您可以尝试在设计视图中添加以下命令字段。
<asp:CommandField ShowDeleteButton="True" />
否则,您必须创建一个按钮模板列以进行删除。
AutoGenerateDeleteButton="false"
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());
}