2017-09-06 93 views
-6

我遇到以下异常无法投型“system.windows.forms.bindingsource”对象键入“system.data.datatable”

无法投类型的对象system.windows .forms.bindingsource为键入” system.data.datatable“

这里是我的方法

private void GridViewStudentsList_RowValidated(object sender, DataGridViewCellEventArgs e) { 
    try { 
     BindingSource bindingSource = (BindingSource) GridViewStudentsList.DataSource; 
     DataTable changes = (DataTable) bindingSource.DataSource; 
     changes.GetChanges(); 
     if (changes != null) { 
      OracleConnection con = new OracleConnection(connection); 
      con.Open(); 
      OracleDataAdapter adapter = new OracleDataAdapter(); 
      OracleCommandBuilder mcb = new OracleCommandBuilder(adapter); 
      adapter.UpdateCommand = mcb.GetUpdateCommand(); 
      adapter.Update(changes); 
      changes.AcceptChanges(); 
      MessageBox.Show("Cell Updated"); 
      con.Close(); 
      return; 
     } 
    } catch (Exception ex) { 
     MessageBox.Show(ex.Message); 
    } 
} 
+0

所以你尝试过调试它和偷看什么类型'bindingSource.DataSource'是什么? – Matten

+0

bindingSource.DataSource ='bindingSource.DataSource'抛出'System.NullReferenceException'类型的异常 – Suriyan

+0

bindingSource.DataSource = {College_Management_System.ViewStudentData} – Suriyan

回答

0

我已经做了一些小错误

下面的代码是工作代码

private void GridViewStudentsList_RowValidated(object sender, DataGridViewCellEventArgs e) 

{ 


     try 

      { 

      BindingSource bindingSource = (BindingSource)GridViewStudentsList.DataSource; 

      DataSet changes = (DataSet)bindingSource.DataSource; 

      changes.GetChanges(); 

      if (changes != null) 
      { 
       OracleConnection con = new OracleConnection(connection); 
       con.Open(); 
       OracleDataAdapter adapter = new OracleDataAdapter("select * from students_list",con); 
       GridViewStudentsList.EndEdit(); 
       OracleCommandBuilder mcb = new OracleCommandBuilder(adapter); 

       adapter.UpdateCommand = mcb.GetUpdateCommand(true); 


       adapter.Update(changes,"students_list"); 
       changes.AcceptChanges(); 

       MessageBox.Show("Cell Updated"); 
       // GridViewStudentsList.EndEdit(); 
       con.Close(); 
       return; 
      } 


     } 

     catch (Exception ex) 
     { 
      MessageBox.Show(ex.StackTrace); 
     } 
    } 
相关问题