Click here to Skip to main content
15,890,845 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please I have two table from database (one to many) .How to display specific fileds from them in datagrid by using dataset.
note: tables created already from database.

code
C#
con = new SqlConnection(str);
            con.Open();
            string custom_tb = "select * from customers";
            string branches_tb = "select * from branches";
            string type_tb="select * from insurance_type";
            dset = new DataSet();
            da = new SqlDataAdapter(custom_tb , con);
            da.Fill(dset, "customers");
            da = new SqlDataAdapter(branches_tb, con);
            da.Fill(dset, "branches");



what is the next
Posted

C#
con = new SqlConnection(str);
            con.Open();
            string custom_tb = "select * from customers UNION ALL select * from branches";
            //string branches_tb = "select * from branches";
            //string type_tb="select * from insurance_type";

            dset = new DataSet();
            da = new SqlDataAdapter(custom_tb , con);
            da.Fill(dset, "customers");
            //da = new SqlDataAdapter(branches_tb, con);
            //da.Fill(dset, "combined");
            DataGridView1.DataSource = dset.Tables["combined"];


If this doesn't solve, study SQL Joins. Here's the link to get you started:
http://www.w3schools.com/sql/sql_join.asp[^]

Please mark as answer and vote 5 if this solved your problem

Regards,
Eduard
 
Share this answer
 
v3
For two separate datagridviews, append the following codes to set the datagrids datasource

C#
DataGridView1.DataSource = dset.Tables["customers"];

DataGridView2.DataSource = dset.Tables["branches"];


Please mark as answer and vote 5 if this solved your problem

Regards,
Eduard
 
Share this answer
 
Comments
mhassan083 2-Dec-11 3:55am    
But i have in the same datagridviews pl.
[no name] 2-Dec-11 4:00am    
so you want to join the tables? do they have foreign keys wherein you can connect the two tables? if not, i'll add a new solution.

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