Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

C#
MySqlConnection conn = new MySqlConnection("SERVER=....");
MySqlDataAdapter ad;
DataSet ds = new DataSet();

ds.Tables.Add("111");
ds.Tables.Add("222");

ad = new MySqlDataAdapter("SELECT * FROM 111", conn);
ad.Fill(ds.Tables["111"]);
datagridview1.DataSource = ds.Tables["111"];

ad = new MySqlDataAdapter("SELECT * FROM 222", conn);
ad.Fill(ds.Tables["222"]);
datagridview2.DataSource = ds.Tables["222"];


'datagridview2' shows table["111"] instead of table["222"].
What am I missing or doing wrong?

What I have tried:

It works with
C#
DataTable dt1, dt2;
MySqlDataAdapter ad1, ad2;


But I don't understand
datagridview2.DataSource = ds.Tables["222"];

shows Table["111"].

Thank you in advance.
Posted
Updated 21-Aug-18 18:18pm

1 solution

Use below code. The reason is when we add using
ds.Tables.Add("111");

it is not able to add table ot ds at 0 and 1 index.

DataTable dt1 = new DataTable();
DataTable dt2 = new DataTable();
MySqlConnection conn = new MySqlConnection("SERVER=....");
MySqlDataAdapter ad;
DataSet ds = new DataSet();

ad = new MySqlDataAdapter("SELECT * FROM 111", conn);
ad.Fill(dt1);


ad = new MySqlDataAdapter("SELECT * FROM 222", conn);
ad.Fill(dt2);

ds.Tables.Add(dt1);
ds.Tables.Add(dt2);

datagridview1.DataSource = ds.Tables[0];
datagridview2.DataSource = ds.Tables[1];
 
Share this answer
 
v2

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