【问题标题】:Data grid view and collections as data source / Issue数据网格视图和集合作为数据源/问题
【发布时间】:2018-02-23 22:34:25
【问题描述】:

我有一个简单的收藏

string[] IDs =  {"1", "2" , "3", "4"};

Collection<TempOBJ> collection1 = reader.Read()<TempOBJ>(IDs);

在前面几行之后,collection1 包含 4 行和几列:

session  |   FirstName  | LastName  |  serial  |  Address |  randomID  |

---------------------------------------------------------------------------

1            Sam               John      d3s3     sample      123

2            Sam               John      d3s3     sample      345

3            Sam               John      6g55     asdfggh     678

4            Joe               Sammy      d7g5     asdfgh      987

然后我将一个网格视图的数据源设置为集合:

GridView1.DataSource = collection1 ;
GridView1.DataBind();

当代码运行时,它会显示所有列。我想要的是特定的列,例如 FirstName、LastName 和 Address。

我该怎么做?

提前致谢

【问题讨论】:

  • 什么是阅读器?
  • 这只是我使用的一种方法,它是自定义的。它用数据填充集合,我希望显示网格视图中的特定字段。

标签: gridview datasource


【解决方案1】:

我找到了解决问题的方法。我将 gridview 的 AutoGenerateColumns 设置为 false 并在标记代码中手动设置列。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:BoundField DataField="FirstName" HeaderText="FirstName" />
                <asp:BoundField DataField="LastName" HeaderText="LastName" />
                <asp:BoundField DataField="Address"  HeaderText="Address"/>
            </Columns>
        </asp:GridView>

后面的代码是一样的。

【讨论】:

    猜你喜欢
    • 2011-09-27
    • 2020-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-16
    相关资源
    最近更新 更多