ASP.向Gridview添加列
本文关键字:添加 Gridview ASP | 更新日期: 2023-09-27 18:09:36
我有一个gridview,显示数据从数据库使用LINQ到SQL。
AssetDataContext db = new AssetDataContext();
equipmentGrid.DataSource = db.equipments.OrderBy(n => n.EQCN);
我需要在girdview的末尾添加一列,该列将具有编辑/删除/查看行的链接。我需要链接为"http://localhost/edit.aspx?"ID=" + idOfRowItem.
尝试在GridView中添加TemplateField:
<Columns>
<asp:TemplateField>
<ItemTemplate>
<a href="http://localhost/edit.aspx?ID=<%# DataBinder.Eval(Container.DataItem, "id") %>">Edit</a>
</ItemTemplate>
</asp:TemplateField>
</Columns>
在项目模板中,您可以放置任何您喜欢的链接,并将您希望从数据源中绑定到它们的任何数据。在上面的例子中,我刚刚从名为id的列中提取了值。
就目前情况而言,这将工作得很好,但是上面的列将在GridView中最左对齐,所有自动生成的列都在其右侧。
要解决这个问题,您可以为RowCreated事件添加一个处理程序,并将该列移动到自动生成列的右侧,如下所示:
gridView1.RowCreated += new GridViewRowEventHandler(gridView1_RowCreated);
...
void gridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
TableCell actionsCell = row.Cells[0];
row.Cells.Remove(actionsCell);
row.Cells.Add(actionsCell);
}