Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

with this code am getting table data on datagridview. but i want multiple table data to retrieve on single datagridview.

in each table userid is common cloumn.

C#
SqlConnection conn = new SqlConnection();
            conn.ConnectionString = Helper.ConnectionString;

            conn.Open();
            SqlDataAdapter da = new SqlDataAdapter("Select UserId,UserName,UserDomainName,UserMachineName,UserIP,UserOsVersion,UserSystemDirectory,UserCurrentDirectory,ProcessorName,ProcessMnufacturer,ProcessorID,ProcessorDescription,ProcessorVersion,ProcessorStatus,ProcessorDeviceId,OSCaption,OSSerialNumber,OSManufacturer,OSVersion,OSStatus,OSName,BiosName,BiosVersion,BiosSerialNumber,BiosManufacturer,BiosCurrentlanguage,BiosStatus FROM SystemInfo Where UserId=@UserId", conn);

            //SqlDataAdapter da = new SqlDataAdapter("Select DisplayName,ServiceName,Status,ServiceType FROM ServiceInfo Where MainId=@MainId", conn);
            {
                da.SelectCommand.Parameters.AddWithValue("@UserId", textBox1.Text);
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;



can anyone help me with sample code example.

Thanks & Regards
sam.198979
Posted
Comments
[no name] 24-Jun-13 8:04am    
Sample code for what? Change your query to get the data from multiple tables.
Rajesh Anuhya 24-Jun-13 8:40am    
but i want multiple table data to retrieve on single datagridview --mean can you explain it more?
jaideepsinh 24-Jun-13 8:44am    
If you want data from multiple table use joins.

Quote:
but i want multiple table data to retrieve on single datagridview

You have created one databale for the query which you mentioned in your code to retrieve from database.
Now, create one more SQL query and datatable to fetch records from another table which you want and add it to newly created datatable.

Now create one more datatable - third one. Add data from first & second to it.

First add columns of datatable 1 & then datatable 2.
and then, add data from each tables to this one.

Once you've done with it, use the new datatable [third one] as a datasource for grid.

For a similar schema datatable, use DataTable.Merge Method[^]
 
Share this answer
 
Comments
sam.198979 27-Jun-13 3:11am    
Thanks ;)
Prasad_Kulkarni 27-Jun-13 7:10am    
Glad it helps!
You're welcome.
Change your select to include the data from the other tables as well:
SQL
SELECT a.ID, a.Col1, b.Col1 
FROM Table1 a
JOIN Table2 b ON a.ID=b.ID
WHERE ID=@ID

If you don't specify a column, it won't be retrieved!
 
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