从不同的线程填充datagridview列 [英] Fill datagridview Column from different thread
问题描述
如何在不同线程的特定列值的datagridview中填充值。
委托更新文本框值但不是gridview。
我尝试使用委托线程但它没有更新。
当我在委托方法中检查RowCount的值时,RowCount总是一个。但是在mainform上Rowcount是正确的。
如何在datagrid中逐行更新
我试过以下代码
How to fill value in datagridview of a particular column value from different thread.
Delegate Updates textbox values but not gridview.
I tried using delegate thread but it does not update.
When I check the value of RowCount in delegate method RowCount is always one.but on mainform Rowcount is correct.
How to update row by row value in datagrid
I tried following code
threadclass
{
void Run ( )
{
for ( int i = 0; ( i <= 10 ); i++ )
{
SR = i;
Value = i.ToString ( );
Program.MainFormObj.Datagridview1.Invoke (
new Del_WriteValueToGridView (
WriteToGridView ),
SR,
Value );
}
}
void WriteToGridView ( int count,
string Value)
{
//column 1
Program.Form2Obj.DataGridView1.
Rows [ count ].
Cells [ 1 ].Value = Value;
}
}
我也尝试在mainform中使用writeValueTogridview()方法,然后在
threadclass中直接调用此方法但不行。
I also tried writeValueTogridview() method in mainform and then directly calling this method in
threadclass but not work.
推荐答案
要从不同的线程写入,你必须使用委托:这是你如何实现你的目标:
Hi,
To write from a diffrent thread you have to use delegates: here is how you can achive your goal:
dataGridView1.Invoke((MethodInvoker)delegate
{
dataGridView1.DataSource = null;
dataGridView1.DataSource = myList.ToList();
});
我在执行任何操作之前通常将datagridview设置为null,以确保重新绘制dataGrid。
i尝试使用dataGridView1.Refresh(),Repaint()......什么都行不通。
刷新dataGridView的最佳方法是如前所述。
希望它有所帮助。
PS:你可以以不同的方式使用代表,但这一个通常是最简单/最快的。
顺便说一下,既然您已经实现了委托,那么您必须确保在表单关闭之前终止该线程,否则您将遇到异常。
I generaly set the datagridview to null before doing any kind of operation to be sure the dataGrid is repainted.
i tryed using dataGridView1.Refresh(), Repaint() ... nothing works.
the best method to refresh a dataGridView is to do as described earlier.
Hope it helps.
PS: you can use delegates in diffrents way, but this one is generaly the easiest/fastest.
By the way, Now that you have implemented delegates, you have to make sure the thread is terminated before the form is closed, otherwise you will have an exception.
这篇关于从不同的线程填充datagridview列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!