简体   繁体   English

如何将dataTable附加到GridView?

[英]How do I append a dataTable to a GridView?

I have a gridView which gets data as such: 我有一个gridView,它获取这样的数据:

DataTable schedulesTable = Transporter.GetDataTableFromAPI(callingURL);
gvSchedules.DataSource = schedulesTable;
gvSchedules.DataBind();

Once this GridView has data, I want to APPEND another dataTable to it. 一旦此GridView包含数据,我想向其追加另一个dataTable。

The problem is, if I do this: 问题是,如果我这样做:

DataTable schedulesTable = Transporter.GetDataTableFromAPI(callingURL); <-- different URL, so new table
gvSchedules.DataSource = schedulesTable;
gvSchedules.DataBind();

It reloads the data into the Grid with JUST this table. 仅使用此表,它将数据重新加载到Grid中。 It eliminates the other one. 它消除了另一个。

How would I go about adding multiple tables to a GridView? 如何将多个表添加到GridView?

Currently your data is probably stored in the VIEWSTATE. 当前,您的数据可能存储在VIEWSTATE中。 That's very expensive. 那很贵。

So, the 1st step is to keep track of the table on the server-side. 因此,第一步是在服务器端跟踪表。 And disable viewstate for the GridView. 并禁用GridView的viewstate。 Andf then you can Merge the new table: 然后,您可以合并新表:

DataTable schedulesTable1 = GetTableFromSessionOrFromGridView(); // whatever is practical
DataTable schedulesTable2 = Transporter.GetDataTableFromAPI(callingURL);  // new table
schedulesTable1.Merge(schedulesTable2);
gvSchedules.DataSource = schedulesTable1;
gvSchedules.DataBind();

Gridviews only show data from 1 table with 1 dataset. Gridviews仅显示来自1个表和1个数据集的数据。

You can either append the second dataset to the first, or you can make a usercontrol with 2 gridviews and bind each with it's own datatable. 您可以将第二个数据集追加到第一个数据集,也可以使用户控件具有2个网格视图,并将每个数据集与它自己的数据表绑定。 or have nested gridviews if the data from the 2 tables has that type of one to many relationship. 或具有嵌套的gridviews(如果2个表中的数据具有这种类型的一对多关系)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM