1

Really I don't know if my doing is correct programmatically or not so I hope to point me out.
I have a DataGridView C# showing data from linked MySQL database and some input boxes to help inserting data into the database and all goes alright, but when I decided to make inserting operation in a separate new thread I do the following inside add button:

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){...}
}

so then a new dialog error is popping-up in each new insert operation and nothing saved, here is the error:
DataGridView default error

I thought the error maybe caused of this conflicting in this.studentinfoTableAdapter.Fill(this.schoolDataSet.studentinfo); I did correct it but the error still there.
Any suggestion could help me out and appreciated.

devduder
  • 394
  • 1
  • 10
wisdom
  • 412
  • 2
  • 5
  • 20
  • Debug your code. See where you get this error. Read [What is IndexOutOfRangeException and how do I fix it?](http://stackoverflow.com/questions/20940979/what-is-indexoutofrangeexception-and-how-do-i-fix-it) And fix it. – Soner Gönül Jun 21 '14 at 11:09
  • I did debuged it the error showed when reaching to `this.studentinfoTableAdapter.Fill(this.schoolDataSet.studentinfo);` line so why I thought `this` conflict but that didn't help – wisdom Jun 21 '14 at 11:11

1 Answers1

0

Finally I've got it, after hours of searching, reading, and trying I found that I should use Invoke to this specific line inside the thread this.studentinfoTableAdapter.Fill(this.schoolDataSet.studentinfo);
so it becomes as following:
this.Invoke(new MethodInvoker(delegate { this.studentinfoTableAdapter.Fill(this.schoolDataSet.studentinfo);}));
I think the error caused because this line calls some features from the main thread to update datagridview while new thread executing and programmatically there exists a cross-thread operation exception so modifying or editing GUI prevented except from main thread so an error arises from thread behavior! it's how I did understand that.
if I'm wrong please point me.

wisdom
  • 412
  • 2
  • 5
  • 20