2016-09-21 65 views
-1

我正在开发一个Word-Addin项目(仅供参考,特别是在性能方面)。我有一个DataGridView,我在运行时填充数据。 问题是我在DataGridView中填充的大量数据(通常以百万为单位)来自webService调用,它在响应该调用时非常困难地需要10-20秒。此外,gridView上还有搜索功能,我不想调用web-Service,因为我知道DataGridView在通过GridView进行搜索时非常高效。我想以异步方式填充数据,以便最终用户不必等待响应,并且word也不会冻结。 我可以使用线程池以异步方式填充数据,但搜索功能将不明确。 任何想法如何处理这种情况? 我感谢任何帮助。 谢谢 关注如何有效地将数据填充到winform DataGridView异步?

+0

所有单词和无码使杰克的问题稍显沉闷。 –

+0

感谢您的反馈意见,我没有包含代码,因为我需要任何替代方案但有效的方法如何做到这一点。我正在执行的是一种常见的方法,显然你知道。我不认为包含代码是有道理的。如果我出错了,请原谅我 –

+0

你是对的。您的特定问题不需要代码来回答,但是如果我们想要帮助您找到更有效的方法,那么包含您当前解决方案的一部分将会有所帮助。附:我不是downvoter。 :p –

回答

0

您可以使用VirtualMode。这样做是 当你的DataGridView想在一次显示它得到它从一个事件不必加载它们人的项目

// Enable virtual mode. 
    this.dataGridView1.VirtualMode = true; 

    // Connect the virtual-mode events to event handlers. 
    this.dataGridView1.CellValueNeeded += new 
     DataGridViewCellValueEventHandler(dataGridView1_CellValueNeeded); 
    this.dataGridView1.CellValuePushed += new 
     DataGridViewCellValueEventHandler(dataGridView1_CellValuePushed); 
    this.dataGridView1.NewRowNeeded += new 
     DataGridViewRowEventHandler(dataGridView1_NewRowNeeded); 
    this.dataGridView1.RowValidated += new 
     DataGridViewCellEventHandler(dataGridView1_RowValidated); 
    this.dataGridView1.RowDirtyStateNeeded += new 
     QuestionEventHandler(dataGridView1_RowDirtyStateNeeded); 
    this.dataGridView1.CancelRowEdit += new 
     QuestionEventHandler(dataGridView1_CancelRowEdit); 
    this.dataGridView1.UserDeletingRow += new 
     DataGridViewRowCancelEventHandler(dataGridView1_UserDeletingRow); 

CellValueNeeded可能看起来像这样

private void dataGridView1_CellValueNeeded(object sender , DataGridViewCellValueEventArgs e) 
{ 
    e.Value = $"Row {e.RowIndex} , Column {e.ColumnIndex}"; 
}