Click here to Skip to main content
16,004,653 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
Hello Guys,! i have created 4 data tables in my project and now i want to merge this all data tables in a single datagridview


What I have tried:

C#
DataSet dsa = new DataSet();
            DataTable dt1 = new DataTable();
            dsa.Tables.Add(dt1);
            OleDbDataAdapter da = new OleDbDataAdapter();
            da = new OleDbDataAdapter("SELECT [Flavours] As [Flavours],count(column2) As [Small] from [Total] Where [Date] between #" + dateTimePicker1.Value.ToString("dd/MM/yyyy") + "# AND #" + dateTimePicker2.Value.ToString("dd/MM/yyyy") + "# AND [column2] IN ('Small.......')Group By [Flavours]", VCON);
            da.Fill(dt1);
            dataGridView1.DataSource = dt1;
            VCON.Close();
            DataSet dsa1 = new DataSet();
            DataTable dt2 = new DataTable();
            dsa1.Tables.Add(dt2);
            OleDbDataAdapter da1 = new OleDbDataAdapter();
            da1 = new OleDbDataAdapter("SELECT [Flavours] As [Flavours],count(column2) As [Medium] from [Total] Where [Date] between #" + dateTimePicker1.Value.ToString("dd/MM/yyyy") + "# AND #" + dateTimePicker2.Value.ToString("dd/MM/yyyy") + "# AND [column2] IN ('Medium......')Group By [Flavours]", VCON);
            da1.Fill(dt2);
            dataGridView1.DataSource = dt2;
            VCON.Close();
            DataSet dsa2 = new DataSet();
            DataTable dt3 = new DataTable();
            dsa.Tables.Add(dt3);
            OleDbDataAdapter da2 = new OleDbDataAdapter();
            da2 = new OleDbDataAdapter("SELECT [Flavours] As [Flavours],count(column2) As [Large] from [Total] Where [Date] between #" + dateTimePicker1.Value.ToString("dd/MM/yyyy") + "# AND #" + dateTimePicker2.Value.ToString("dd/MM/yyyy") + "# AND [column2] IN ('Large.......')Group By [Flavours]", VCON);
            da2.Fill(dt3);
            dataGridView1.DataSource = dt3;
            VCON.Close();
            DataSet dsa3 = new DataSet();
            DataTable dt4 = new DataTable();
            dsa.Tables.Add(dt1);
            dsa.Tables.Add(dt2);
            dsa.Tables.Add(dt3);
            dsa.Tables.Add(dt4);
            OleDbDataAdapter da3 = new OleDbDataAdapter();
            da3 = new OleDbDataAdapter("SELECT [Flavours] As [Flavours],count(column2) As [Ex Large] from [Total] Where [Date] between #" + dateTimePicker1.Value.ToString("dd/MM/yyyy") + "# AND #" + dateTimePicker2.Value.ToString("dd/MM/yyyy") + "# AND [column2] IN ('Ex  Large...')Group By [Flavours]", VCON);
            da3.Fill(dt4);
            dataGridView1.DataSource = dt4;
            VCON.Close();
Posted
Updated 28-Sep-16 2:46am
v3
Comments
[no name] 28-Sep-16 8:36am    
Okay good. You have permission to proceed. No one here is going to stand in your way.
Suvendu Shekhar Giri 28-Sep-16 8:40am    
What is the issue?
Member 9983063 28-Sep-16 8:44am    
issue is i want to merge all data tables in a single datagridview that's it
Suvendu Shekhar Giri 28-Sep-16 8:45am    
and with what you have tried, are you facing any error or issue?

1 solution

The best way to do this is to use a single SELECT which combines the tables into a single set of columns - otherwise the DGV doesn't know what to do with it.
Generally, you would have common data in the tables - a foreign key or two - which lets SQL know which rows are related via a JOIN query:
SQL
SELECT u.UserName, a.Address FROM Users u
JOIN Addresses a ON a.UserID = u.ID
Which returns each user and his address from two tables connected via teh UserID foreign key into the Users table.

If you don't have such relationships, then pretty much you can't do it as SQL and teh DGV have no idea which rows to "bolt together" to display.
 
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