尝试像这样将 TemplateField 添加到您的 GridView:
<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);
}
希望这会有所帮助。