I’m studying delegates and simple threading, I tried it in a ComboBox control, and experimented in a DataGridViewComboBoxColumn (cause I thought it would be the same) but it seems there’s no Invoke property for this kind.
How can I set DataGridViewComboBoxColumn properties in a thread?
Please see my code, this works for setting the properties of a ComboBox control using a thread:
private delegate void DelegateSetProperties(DataTable dataSource, string valueMember, string displayMember);
Thread thread1;
DelegateSetProperties delegateSetProperties;
private void Form1_Load(object sender, EventArgs e)
{
delegateSetProperties = new DelegateSetProperties(SetProperties);
thread1 = new Thread(new ThreadStart(InitValues));
thread1.IsBackground = true;
thread1.Start();
}
private void SetProperties(DataTable dataSource, string valueMember, string displayMember)
{
comboBox1.DataSource = dataSource;
comboBox1.ValueMember = valueMember;
comboBox1.DisplayMember = displayMember;
comboBox1.SelectedIndex = 0;
//dataGridViewComboBoxColumn1.DataSource = dataSource;
//dataGridViewComboBoxColumn1.DisplayMember = valueMember;
//dataGridViewComboBoxColumn1.ValueMember = displayMember";
}
void InitValues()
{
var dt = new DataTable
{
TableName = "CATEGORY",
Columns = {
{"CategoryCode", typeof(string)},
{"Name", typeof(string)},
}
};
dt.Rows.Add("C1", "Category1");
dt.Rows.Add("C2", "Category2");
dt.Rows.Add("C3", "Category3");
// and so on...
comboBox1.Invoke(delegateSetProperties, new object[] { dt, "CategoryCode", "Name"
//dataGridViewComboBoxColumn1.Invoke(delegateSetEvents, new object[] { dt, "CategoryCode", "Name" });
});
}
Please help…thanks in advance.
Create a function as shown below
Call it like
OR
If you’re using .NET 3.5 or above, you could rewrite the above method as an extension method of the Control class, which would then simplify the call to:
OR
Try this
Stackoverflow Reference : How to update the GUI from another thread in C#?