如何在asp.net自动生成列的www.example.com网格视图中添加新列?

l7mqbcuq  于 2022-12-05  发布在  .NET
关注(0)|答案(2)|浏览(138)

How to add new hyperlink column to an asp.net gridview where columns are autogenerated? The columns are not predefined in the gridview.

dwbf0jvd

dwbf0jvd1#

只需将您的列定义添加到网格视图的部分中。您自动生成的列应该显示在此视图的左侧。

<asp:gridview AutoGenerateColumns="true" ... >
    <columns>
        <asp:hyperlink ... />
    </columns>
</asp:gridview>
nwnhqdif

nwnhqdif2#

我发现自动生成的列显示在右侧。如果希望它们显示在左侧,则必须向RowCreated事件添加代码,该事件删除并重新添加所有列,如下所示:

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
  {
        GridViewRow row = e.Row;
        List<TableCell> columns = new List<TableCell>();

        foreach (DataControlField column in GridView1.Columns)
        {
            TableCell cell = row.Cells[0];
            row.Cells.Remove(cell);
            columns.Add(cell);
        }

        row.Cells.AddRange(columns.ToArray());
    }

在此处找到文章:http://geekswithblogs.net/dotNETvinz/archive/2009/06/03/move--autogenerate-columns-at-leftmost-part-of-the-gridview.aspx

相关问题