Click here to Skip to main content
15,905,148 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have 2 table,tb1 and tb2
how do i fatch both table data in 1 gridview?
tbl1 field ----NO,NAME
TBL2 field-----NO,CITY,PHONE
I NEED NO NAME CITY PHONE IN GRIDVIEW

THNK U ADVV
Posted
Updated 26-Jun-16 23:03pm
v2

Use sqljoins[^] to get the columns from both tables and bind gridview.

or check here[^]
 
Share this answer
 
Comments
[no name] 11-Feb-11 1:15am    
Good Call My 5
See here[^].
 
Share this answer
 
You can use column aliases to name the columns that you fetch from the database. Using column aliases, you can link the alias name with the column of the DataGridView control.

First, you'll have to fetch the data with column aliases from the database and store it in a DataTable. You can use the following code for that:

C#
SqlConnection cn = new SqlConnection (...);

string sql = "Select p.no as 'Num', p.Name as 'Name', q.city as 'City', q.Phone as 'Phone' from tab1 p, tab2 q; where p.no = q.no";

SqlCommand sCmd = new SqlCommand(sql,cn);
SqlDataAdapter sAdap = new SqlDataAdapter(sCmd);
DataSet ds = new DataSet();

sAdap.Fill(ds);

DataTable dt = ds.Tables[0];


Now create your DataGridview. Choose Edit Columns (in the Component menu) and add four columns. You can name the columns or provide headers based on your wish. For the DataProperty Value of each of the columns provide values as Num, Name, City and Phone for the columns respectively.

Now set the DataSource of the DataGridView to the Datatable. (Refer the code below).

C#
this.DataGridView1.DataSource = dt;



That should do it!

~ Cheers
 
Share this answer
 
Hope http://www.sql-tutorial.net/SQL-JOIN.asp[^] also will give you an idea and also check out the mkgoud links all are very good links.
 
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