Click here to Skip to main content
15,917,565 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Friends,

Please help me,

I have three datasets and i want to bind three dataset values into one dataset and bind this dataset to gridview.

Give me any solution.
Posted

For combining data from multiple datasets, all datasets must have same structure.
C#
DataSet ds1 = new DataSet();
DataSet ds2 = new DataSet();
DataSet ds3 = new DataSet();

//Code to fill three datasets here

DataSet ds = new DataSet();
DataTable dt = ds1.Tables[0].Clone();
ds.Tables.Add(dt);

foreach (DataRow dr in ds1.Tables[0].Rows)
    ds.Tables[0].ImportRow(dr);

foreach (DataRow dr in ds2.Tables[0].Rows)
    ds.Tables[0].ImportRow(dr);

foreach (DataRow dr in ds3.Tables[0].Rows)
    ds.Tables[0].ImportRow(dr);

//ds will have data of first datatable of all three datasets ds1,ds2,ds3. Bind ds to gridview
 
Share this answer
 
v3
Comments
nita kadam 9-Aug-13 7:27am    
ds.Tables[0].ImportRow(dr); gets error like cannot fine table 0
Gautam Raithatha 9-Aug-13 7:39am    
yes you need to create datatable with same structure as three dataset's datatables and add it to ds.

check updated solution.
nita kadam 9-Aug-13 8:09am    
A DataTable named 'Table' already belongs to this DataSet i got this error for second dataset.
nita kadam 9-Aug-13 8:11am    
A DataTable named 'Table' already belongs to this DataSet, I got error like this in second dataset
Gautam Raithatha 9-Aug-13 8:13am    
you don't have to add datatable dt to dataset ds for three times. just add dt to ds once. Check above solution.
You can use Merge function if all the dataset are identical in-terms of column Name & No. of column

Like
ds1.merge(ds2)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900