desifunde

Hi,

On adding a datarow to a datatable which is tied to a DataGridView through a binding source, I am receiving a "A first chance exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll"

What I am doing is

Main Thread:

DataGridView.datasource = bindingsource1
bindingsource1.datasource = datatable1
thread2.start(addressof proc)


sub proc
{
mainform.mydelegate.invoke(object o)
}

the mainform has a delegate called mydelegate which suspends the binding of the Datagridview, adds the row and resumes the binding

any help is appreciated.

Thanks



Re: Windows Forms Data Controls and Databinding Can I modify the datatable which a datagridview is bound to using a databinding ?

ScubaSteve20001

It looks like you are still trying to update the Form from a different thread. Try this:

mainform.invoke(mainform.mydelegate, new object[] {object o})






Re: Windows Forms Data Controls and Databinding Can I modify the datatable which a datagridview is bound to using a databinding ?

desifunde

Thanks for your reponse.

I have tried the invoke() call also, but I still get the exception. The only way I cannot get an exception is, if I set the datasource for the dgview, add results to the datatable and then I call form.show(). Once the form ( and dgview) and visible, adding any data to the underlying datatable causes exceptions.




Re: Windows Forms Data Controls and Databinding Can I modify the datatable which a datagridview is bound to using a databinding ?

Rong-Chun Zhang - MSFT

Hi desifunde,

Access to Windows Forms controls is not inherently thread safe. If you have two or more threads manipulating the state of a control, it is possible to force the control into an inconsistent state. Other thread-related bugs are possible as well, including race conditions and deadlocks. It is important to ensure that access to your controls is done in a thread-safe way.

The .NET Framework helps you detect when you are accessing your controls in a manner that is not thread safe. When you are running your application in the debugger, and a thread other than the one which created a control attempts to call that control, the debugger raises an InvalidOperationException with the message, "Control control name accessed from a thread other than the thread it was created on."

Try

Code Snippet

namespace DataManage

{

public partial class ChangeDataSource_MultiThread : Form

{

public ChangeDataSource_MultiThread()

{

InitializeComponent();

}

DataTable dt = new DataTable();

BindingSource bs = new BindingSource();

delegate void AddRow(DataRow dr);//important

private void ChangeDataSource_MultiThread_Load(object sender, EventArgs e)

{

dt.Columns.Add("a");

dt.Columns.Add("b");

dt.Columns.Add("c", typeof(bool));

for (int j = 0; j < 10; j++)

{

dt.Rows.Add("a" + j.ToString(), "escddf" + j.ToString(), j % 2 == 0);

}

bs.DataSource = dt;

this.dataGridView1.DataSource = bs;

}

private void button1_Click(object sender, EventArgs e)

{

Thread a = new Thread(Method_add);

a.Start();

}

private void Method_add()

{

DataRow dr = this.dt.NewRow();

dr[0] = "aaaa";

dr[1] = "bbbbb";

dr[2] = true;

addrow(dr);

}

private void addrow(DataRow dr)

{

if (this.dataGridView1.InvokeRequired)

{

AddRow ad = new AddRow(addrow);

this.Invoke(ad, new object[] { dr });

}

else

this.dt.Rows.Add(dr);

}

}

}

Hope this helps.

Regards






Re: Windows Forms Data Controls and Databinding Can I modify the datatable which a datagridview is bound to using a databinding ?

desifunde

Thanks.

So am I correct in understanding that getting the InvalidOperationException(s) when running the code in the debugger is just to inform the programmer that the control is being accessed from a different thread (even though invoke() is being used), and is not a bug




Re: Windows Forms Data Controls and Databinding Can I modify the datatable which a datagridview is bound to using a databinding ?

Rong-Chun Zhang - MSFT

Hi desifunde,

I thinks so.

Regards






Re: Windows Forms Data Controls and Databinding Can I modify the datatable which a datagridview is bound to using a databinding ?

desifunde

Thanks

I am working on another issue, after binding a datagridviewcomboboxcolumn, the default entry displayed is empty for all datagridviewcomboboxcell, how can I change this to display the value at the first index by default

Which event would I need to capture and set the shown value in .






Re: Windows Forms Data Controls and Databinding Can I modify the datatable which a datagridview is bound to using a databinding ?

Rong-Chun Zhang - MSFT

Hi desifunde,

Try to handle DataGridView.EditingControlShowing Event to get the combo box which hosted in the cell, then change the SelectedIndex of this combo box.

Hope this helps.

Regards






Re: Windows Forms Data Controls and Databinding Can I modify the datatable which a datagridview is bound to using a databinding ?

desifunde

I thought this event was only fired when the user clicks on the cell to edit it, I want to show the first item by default on all comboboxcells not just the one on which the user clicked/is editing

Can I still use this event for that purpose ON trying to set the selectedindex, the first item is still empty