Click here to Skip to main content
11,928,078 members (48,637 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# C#4.0

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.

SqlConnection conn = new SqlConnection();
            conn.ConnectionString = Helper.ConnectionString;
            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();
                dataGridView1.DataSource = dt;

can anyone help me with sample code example.

Thanks & Regards
Posted 24-Jun-13 3:02am
ThePhantomUpvoter 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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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[^]
sam.198979 27-Jun-13 3:11am
Thanks ;)
Prasad_Kulkarni 27-Jun-13 7:10am
Glad it helps!
You're welcome.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Change your select to include the data from the other tables as well:
SELECT a.ID, a.Col1, b.Col1 
FROM Table1 a
JOIN Table2 b ON a.ID=b.ID
If you don't specify a column, it won't be retrieved!

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.151126.1 | Last Updated 24 Jun 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100