2014-06-21 58 views
1

真的,我不知道我的做法是否以編程方式是正確的,所以我希望指出我。
我有一個顯示來自鏈接MySQL數據庫和一些輸入框數據的DataGridView C#來幫助數據插入到數據庫中,一切都還好,但是當我決定做一個單獨的新線程插入操作我做了以下內部添加按鈕:DataGridView在新線程內的默認錯誤

private void AddToolStripMenuItem_Click(object sender, EventArgs e){ 
    try{ 
     new thread(() =>{ 
      // insertion code goes here 
      //to update DataGridView after inserting 
      this.studentinfoTableAdapter.Fill(this.schoolDataSet.studentinfo);   
     }).start(); 
    }catch(exception ex){...} 
} 

所以後來一個新的對話框錯誤是彈出式在每一個新的插入操作並沒有什麼保存,這裏是錯誤:
DataGridView default error

我想在this.studentinfoTableAdapter.Fill(this.schoolDataSet.studentinfo);衝突的可能造成的this錯誤我確實糾正了它,但錯誤仍然存​​在。
任何建議可以幫助我,並讚賞。

+0

調試你的代碼。看看你得到這個錯誤的地方。閱讀[什麼是IndexOutOfRangeException,如何解決它?](http://stackoverflow.com/questions/20940979/what-is-indexoutofrangeexception-and-how-do-i-fix-it)並解決它。 –

+0

我做了debuged錯誤顯示時,達到'this.studentinfoTableAdapter.Fill(this.schoolDataSet.studentinfo);'線爲什麼我認爲這'衝突,但沒有幫助 – wisdom

回答

0

最後,我知道了, 搜索,閱讀了幾個小時之後,並嘗試我發現,我應該用Invoke這個特定的行線程內this.studentinfoTableAdapter.Fill(this.schoolDataSet.studentinfo);
,使其成爲爲:
this.Invoke(new MethodInvoker(delegate { this.studentinfoTableAdapter.Fill(this.schoolDataSet.studentinfo);}));
我覺得造成錯誤,因爲該行調用一些功能從主線程,而新的線程中執行更新的DataGridView和程序存在cross-thread operation exception如此修改或防止除了從主線程編輯界面使錯誤從線程的行爲出現!這是我的理解。
如果我錯了,請指出我。