Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datagridview with two datasources

I have two tables one is them main table contains (id, Date, TestID, Result) and the orther is a lookup table (TestID, TestName)

I want to show data like this in datagridview:

Id , Date, Test 1, Test 2, Test 3, Result.

and to be able to save it, is this possible? and how to start

like image 286
NoranTawfeek Avatar asked Dec 20 '25 14:12

NoranTawfeek


1 Answers

Perhaps you could combine the data from each of your datasources into a single dataset and then assign that dataset as the source of your dataview. However, without further info it's hard to speculate on possible solutions.

However ..

You can combine your resultsets into one DataTable using two SqlDataAdapter to fill the DataTable from the two databases respectively. Here is an example.

    DataTable dt = new DataTable();
    using(SqlDataAdapter a1 = new SqlDataAdapter("SELECT * FROM [user1]", "Data Source=DBServer1;Initial Catalog=Database1;User ID=user;Password=***"))
    a1.Fill(dt);

    using(SqlDataAdapter a2 = new SqlDataAdapter("SELECT * FROM [user2]", "Data Source=DBServer2;Initial Catalog=Database2;User ID=user;Password=***"))
    a2.Fill(dt);

***ABOVE IS JUST EXAMPLE its not the 100% correct method to do it starting with the connection to the Database , but its something for you to start and think about it ****

a1.Fill(dt) will initialize the DataTable and fill it. a2.Fill(dt) just adds rows to the DataTable dt from the other resultset. This example assumes that the two data sources have the same schema. If not, you have to prepare the datatable to accomodate both resultsets.

Hope this helps.

like image 70
Romero Avatar answered Dec 22 '25 02:12

Romero



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!