简体   繁体   English

在datagridview中添加新行后,立即显示它

[英]After adding a new row in datagridview, display it immediately

I have a datagridview. 我有一个datagridview。 When I programmatically add a new row, it should appear immediately on datagridview, instead of waiting to finish all the rows I have. 当我以编程方式添加新行时,它应该立即出现在datagridview上,而不是等待完成我拥有的所有行。

I was working with the solution below 我正在使用以下解决方案

...
adapter.Fill(ds);
foreach (DataColumn dc in ds.Tables[0].Columns)
{
    datagridView1.Columns.Add(dc.ColumnName, dc.ColumnName);
}
foreach (DataRow row in ds.Tables[0].Rows)
{
    datagridView1.Rows.Add(row.ItemArray);
}
...

On this solution I have to wait until the ds.Tables[0].Rows finishes, and after that the datagridview display those rows. 在此解决方案上,我必须等待ds.Tables[0].Rows完成,然后datagridview显示这些行。

I am asking for for a solution or suggestion where I can see live rows inserting on datagridview, similar with results on SQL Server after executing a query. 我在寻求解决方案或建议,可以在datagridview上看到活动行,类似于执行查询后在SQL Server上的结果。

The reason while the rows are not added immediately is the UI thread is busy executing the loop, it has no chance to repaint the UI while the loop has not finished. 未立即添加行的原因是UI线程正忙于执行循环,因此在循环未完成时没有机会重新绘制UI。

Solutions: 解决方案:

  1. The old Application.DoEvents() "magic" call. 旧的Application.DoEvents() “魔术”调用。

<= There are a lot of people saying Application.DoEvents are evil, I can get down votes for this. <=有很多人说Application.DoEvents是邪恶的,对此我可以投下反对票。

Back in the early days of .NET I was really impressed by this magic call. 在.NET的早期,这个神奇的电话给我留下了深刻的印象。 This call pauses the on-going task (adding rows), and executes the pending UI stuffs like repainting the UI, processing mouse/keyboard events. 该调用将暂停正在进行的任务(添加行),并执行待处理的UI内容,例如重新绘制UI,处理鼠标/键盘事件。 So the UI can remain responsive, otherwise it will be frozen until the lengthy task has finished. 因此,UI可以保持响应状态,否则它将被冻结,直到完成冗长的任务。

So the code looks like this - Of course, calling it once for each row would be a performance disaster as explained in the comments. 因此,代码看起来像这样-当然,如注释中所述,每行调用一次将导致性能下降。

foreach (DataRow row in ds.Tables[0].Rows)
{
    datagridView1.Rows.Add(row.ItemArray);
    Application.DoEvents(); 
}
  1. Use BeginInvoke , which accepts a delegate. 使用BeginInvoke ,它接受委托。

The difference between the BeginInvoke and directly executing the loop on the UI thread is BeginInvoke is asynchronous. BeginInvoke与直接在UI线程上执行循环之间的区别是BeginInvoke是异步的。 Here is an article explaining BeginInvoke . 这是一篇解释BeginInvoke 的文章 (WOW, I did not expect it has been available since .NET 2.0, a time like a century ago). (哇,我没想到它自.NET 2.0以来(一个世纪前的时代)就已经可用)。

Here is an example of adding rows to DataGridView using BeginInvoke . 这是一个使用BeginInvoke将行添加到DataGridView的示例

Or just use Databind? 还是只使用Databind?

private DataTable Dt = new DataTable();
dataGridView1.DataSource = Dt;
//do what ever
DataRow dataRow = Dt.NewRow();
//add data here to data row
Dt.Rows.Add(dataRow);

Though this wont fix you problem with your GUI locking, I suggest reading this . 虽然这不会解决你与你的GUI锁定的问题,我建议您阅读

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM