Home > Net >  [WinForm] ComboBox bundle an empty line after the DataTable component, dynamic increase DataRow oper
[WinForm] ComboBox bundle an empty line after the DataTable component, dynamic increase DataRow oper

Time:09-18

The ComboBox components to bind to an already initialized structure but not empty line DataTable on assignment, and then the dynamic increase DataRow, graphic effect is correct, but operation error,
The code is as follows,
 
Private void button1_Click (object sender, EventArgs e)
{
DtTest=new DataTable ();
DtTest. Columns. The Add (" ID ");
ComboBox1. DisplayMember="ID";
ComboBox1. ValueMember="ID";
ComboBox1. The DataSource=dtTest;
DtTest. Rows. The Add (new Object [] {" 1 "});
}

Run the code
 dtTest. Rows. The Add (new Object [] {" 1 "}); Output window after 
tip:
In the System. For the first time in the ArgumentOutOfRangeException occasional "System. Windows. Forms. DLL" type of exception

One who can help see

CodePudding user response:

Datatable add columns, rows, then the binding combobox
DtTest=new DataTable ();
DtTest. Columns. The Add (" ID ");
DtTest. Rows. The Add (new Object [] {" 1 "});
ComboBox1. DisplayMember="ID";
ComboBox1. ValueMember="ID";
ComboBox1. The DataSource=dtTest;

CodePudding user response:

reference 1st floor ManBOyyy response:
datatable add columns, rows, and the binding combobox
DtTest=new DataTable ();
DtTest. Columns. The Add (" ID ");
DtTest. Rows. The Add (new Object [] {" 1 "});
ComboBox1. DisplayMember="ID";
ComboBox1. ValueMember="ID";
ComboBox1. The DataSource=dtTest;

I know that increase the line first, then the binding is no problem, just want to know why the binding empty line after the dynamic increase suggests that such a mistake again, also didn't find the related instructions, found only one said there was a thread and the interface between the question, also didn't say specifically,
Is there a big under analysis

CodePudding user response:

refer to the second floor mf1983 response:
... Found only one said there was a thread, and the interface between the problem, also didn't say specific,
Have bosses analysis under


If from the UI thread for dtTest Rows. The Add (), under the WinForm will appear across the problem of the UI thread calls,
In this case, want to use the Control. The Invoke () or SynchronizationContext. The Send (),
 public partial class Form1: Form 
{
//...
Void thread task ()
{
Var id=time-consuming task _ get id ();
This. Invoke (delegate (Action)
{
DtTest. Rows. The Add (new Object [] {id});
});
}
}

CodePudding user response:

reference github_36000833 reply: 3/f
...

Note: when dtTest is to bind to the UI elements, update dtTest need to Control from the UI thread. The Invoke (),

CodePudding user response:

reference 4 floor github_36000833 response:
Quote: reference github_36000833 reply: 3/f

.

Note: when dtTest is to bind to the UI elements, update from the UI thread dtTest need to Control the Invoke (),

Thank you for your reply, the problem of the invoke I know, but the datatable problem is not the cause of the invoke, you see I will give the test code, is simply a button button press event,
  •  Tags:  
  • C #
  • Related