0

On my Page_Load event

DataSet oDs = new DataSet();
oDs.ReadXml("http://stackoverflow.com/feeds");
gvFeeds.DataSource = oDs.Tables[2];
gvFeeds.DataBind();

This populates the GridView as shown below

enter image description here

The DataSet contains 8 DataTables: Feed, Title, Link, Entry, Rank, Category, Author, Summary

DataTable[2] for example, contains the 30 most recent post's Links, as shown above.


What I want is for the GridView to show each records Feed, Title, Link, Entry, Rank, Category, Author, Summary

But, when I assign the enter DataSet to the DataSource, it just populates the GridView as if I had assigned it the DataTable[0] object... being the parent feed's header information.

4

2 回答 2

2

您只能将 GridView 绑定到一个表。例如,使用以下将绑定到“链接”表。

gvFeeds.DataSource = oDs;
gvFeeds.DataMember = "Link";
gvFeeds.DataBind();

但是,如果您的所有表共享相同的模式(不太可能),那么您可以通过转储行手动创建一个新的 DataTable,然后绑定它。

这篇文章中还描述了另一种场景将 两个数据库中的数据添加到一个网格视图中

于 2013-10-08T19:51:29.463 回答
0

无法将来自不同表的数据组合在一起,并一次将其绑定到单个 gridView。

编辑:

        //Considering you have all the tables that share the same schema
        //i.e. All are having same column names and types

        //Create an In Memory DataTable
        DataTable consolidateTable = new DataTable("ConsolidatedTable");

        //Add columns with that is same across all of you tables
        consolidateTable.Columns.Add(new DataColumn("firstColumn"));
        consolidateTable.Columns.Add(new DataColumn("secondColumn"));
        //etc..

        //Now go through each record of each table
        foreach (DataTable dt in oDs.Tables)
        {
            foreach (DataRow row in dt.Rows)
            {
                consolidateTable.Rows.Add(row);
            }
        }

        //bind
        gvFeeds.DataSource = consolidateTable;
        gvFeeds.DataBind();

请考虑这只有在所有表都具有相同架构(即相同的 firstColumn、secondColumn 等)时才有可能。否则,这是不可能的。

于 2013-10-10T17:13:37.390 回答