2015-12-15 4 views
0

У меня проблема при попытке заполнить второй DataGridview из моей базы данных, Первый 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(); 
+1

Что ошибка вы получаете? – Brian

+0

Нет ошибки, проблема в том, что внутри dataGridView «DGV1» есть данные «DGV2», а переменная MyVar2 сохраняет значение «DGV2». – John

+0

_DGV2.DataSource = ds.Tables [0]; _ ?? _DGV2.DataSource = ds.Tables [1]; _?! – TaW

ответ

-1

Попробуйте добавить DGV.DataBind(), как это:

DGV2.DataSource = ds.Tables[0]; 
DGV2.DataBind(); 

и

DGV1.DataSource = ds2.Tables[0] 
DGV1.DataBind(); 
+0

Нет DataBind() для Winforms DataGridView, только для ASP.NET GridView. – HardCode

+0

dataGridView не содержит определения «DataBind» .... если я использую DataBind(); – John

Смежные вопросы