ASP.NET添加列的GridView [英] ASP.NET Add column to Gridview

查看:182
本文介绍了ASP.NET添加列的GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个gridview使用LINQ to SQL显示数据库中的数据。

I have a gridview that is displaying data from a database using LINQ to SQL.

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

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

I need to add a column at the end of the girdview that will have links to edit/delete/view the row. I need the link to be "http://localhost/edit.aspx?ID=" + idOfRowItem.

推荐答案

GridView的TemplateField如下:

Try adding a TemplateField to your GridView like so:

<Columns>
    <asp:TemplateField>
        <ItemTemplate>
            <a href="http://localhost/edit.aspx?ID=<%# DataBinder.Eval(Container.DataItem, "id") %>">Edit</a>
        </ItemTemplate>
    </asp:TemplateField>
</Columns>

在项目模板中,您可以放置​​任何你喜欢的链接,并绑定任何你想要的数据从你的数据源。在上面的例子中,我刚从一个名为id的列中取值。

Within the item template you can place any links you like and bind any data you wish to them from your DataSource. In the example above I have just pulled the value from a column named id.

根据情况,这将很好,但是上面的列会在左边的GridView与其右侧的所有自动生成的列。

As things stand this would work fine, however the column above would be aligned left most in the GridView with all the auto generated columns to its right.

要修复此问题,您可以为RowCreated事件添加一个处理程序,并将列移动到自动生成列的右侧像这样:

To fix this you can add a handler for the RowCreated event and move the column to the right of the auto generated columns like so:

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);
}

希望这有帮助。

这篇关于ASP.NET添加列的GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆