【发布时间】:2015-12-15 14:57:52
【问题描述】:
当我尝试从我的数据库中填充第二个 dataGridview 时遇到问题, 如果我填充第二个 dataGridView,第一个 dataGridView 包含第二个 dataGridView 的值。
我的错误在哪里?
OleDbConnection connection = new OleDbConnection();
DataSet ds = new DataSet();
DataSet ds2 = new DataSet();
OleDbDataAdapter oledbAdapter;
OleDbDataAdapter oledbAdapter2;
//Connection
try
{
connection.ConnectionString = ConfigurationManager.ConnectionStrings["DBCONNECTION"].ConnectionString;
}
catch (Exception er)
{
MessageBox.Show("Errore " + er);
}
//First DataGridView
try
{
string query2 = "select * from MyTable";
connection.Open();
oledbAdapter2 = new OleDbDataAdapter(query2, connection);
oledbAdapter2.Fill(ds2);
DGV1.DataSource = ds2.Tables[0]
}
catch (Exception er)
{
MessageBox.Show("Errore " + er);
}
connection.Close();
//correct assign of value
string F = DGV1.Rows[0].Cells[2].Value.ToString();
//Second DataGridView
{
string query = "select * from MyTable2";
connection.Open();
oledbAdapter = new OleDbDataAdapter(query, connection);
oledbAdapter.Fill(ds);
DGV2.DataSource = ds.Tables[0];
//correct assign of value
string B = DGV2.Rows[0].Cells[9].Value.ToString();
}
catch (Exception er)
{
MessageBox.Show("Errore " + er);
}
connection.Close();
MyVar = DGV2.Rows[0].Cells[9].Value.ToString();
//Incorrect assign of value for DGV1, inside there are the value of DGV2 the last DataGridView Popolate WHY??????
MyVar2 = DGV1.Rows[0].Cells[2].Value.ToString();
【问题讨论】:
-
您遇到的错误是什么?
-
没有错误,问题是de dataGridView "DGV1"里面有"DGV2"的数据,而变量MyVar2保持的是"DGV2"的值。
-
DGV2.DataSource = ds.Tables[0]; ?? DGV2.DataSource = ds.Tables[1]; ?!
-
表[1] 不存在.....
-
ds, ds2, DGV1, DGV2, Adapter1, Adapter2 ... 给你的对象起有意义的名字,你会避免很多问题。
标签: c# winforms datagridview