JJ.
JJ.

Reputation: 9960

How do I append a dataTable to a GridView?

I have a gridView which gets data as such:

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

Once this GridView has data, I want to APPEND another dataTable to it.

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. It eliminates the other one.

How would I go about adding multiple tables to a GridView?

Upvotes: 1

Views: 2950

Answers (2)

fnostro
fnostro

Reputation: 4591

Gridviews only show data from 1 table with 1 dataset.

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. or have nested gridviews if the data from the 2 tables has that type of one to many relationship.

Upvotes: 1

Henk Holterman
Henk Holterman

Reputation: 273264

Currently your data is probably stored in the 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. 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();

Upvotes: 1

Related Questions