次の列を持つグリッドビューがあります
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());
}