【问题标题】:ASP.NET Add column to GridviewASP.NET 将列添加到 Gridview
【发布时间】:2011-09-22 20:33:52
【问题描述】:

我有一个网格视图,它使用 LINQ to SQL 显示来自数据库的数据。

AssetDataContext db = new AssetDataContext();
equipmentGrid.DataSource = db.equipments.OrderBy(n => n.EQCN);

我需要在 girdview 的末尾添加一列,该列将包含用于编辑/删除/查看该行的链接。我需要链接为“http://localhost/edit.aspx?ID=" + idOfRowItem。

【问题讨论】:

标签: c# asp.net gridview


【解决方案1】:

尝试像这样将 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);
}

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2010-12-23
    • 2014-04-10
    • 2011-03-01
    • 2017-08-03
    • 2016-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-19
    相关资源
    最近更新 更多