迭代DataTable.DataColumns非常慢 [英] Iterating DataTable.DataColumns very slow
本文介绍了迭代DataTable.DataColumns非常慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经搜索了问题,并为此搜索了google,但找不到任何东西.我创建了一个使用自定义GridView的C#.NET Forms应用程序.在此GridView中,我读取了自定义过滤器的所有列名称.我使用以下代码执行此操作:
Hi,
I''ve searched the questions and google for this but couldn''t find anything. I have created a C# .NET Forms application that uses a custom GridView. In this GridView I read all column names for a custom filter. I do this with this code:
// _AllData = DataTable
// _FilterColumns = List<filtercolumnclass>
int index = 0;
foreach (DataColumn col in _AllData.Columns)
{
bool found = false;
foreach (FilterColumnClass filter in _FilterColumns)
{
if (filter.ColumnName == col.ColumnName)
{
found = true;
filter.Index = index;
}
}
if (!found)
{
FilterColumnClass new_filter = new FilterColumnClass();
new_filter.ColumnName = col.ColumnName;
new_filter.Index = index;
new_filter.FilterCheckControl.eventAskCurrentDataTable += new AskDataTableByInt(FilterCheckControl_eventAskCurrentDataTable);
new_filter.FilterCheckControl.eventAskSourceDataTable += new AskDataTableByInt(FilterCheckControl_eventAskSourceDataTable);
new_filter.FilterCheckControl.eventApplyFilter += new EventHandler(FilterCheckControl_eventApplyFilter);
_FilterColumns.Add(new_filter);
this.Controls.Add(new_filter.FilterCheckControl);
}
index++;
}
</filtercolumnclass>
它可以工作,但是非常慢,一个具有约80列的数据表将需要大约200毫秒的迭代时间.所以我的问题是,我该如何加快速度?
It works, but it''s very slow, a datatable with about 80 columns will take about 200 ms to iterate. So my question is, how can I speed this up?
推荐答案
尝试SuspendLayout可以加快速度. (添加了一些额外的代码,有望加快该过程)
Try SuspendLayout to speed it up. (Added some extra code that hopefully speeds up the process)
this.SuspendLayout();
try
foreach (DataColumn col in _AllData.Columns)
{
FilterColumnClass fc = _FilterColumns.Find(delegate(FilterColumnClass item) { return item.ColumnName == col.ColumnName; });
if (fc != null)
{
filter.Index = index;
}
{
FilterColumnClass new_filter = new FilterColumnClass();
// ...
}
} finally
{
this.ResumeLayout();
}
祝你好运!
Good luck!
这篇关于迭代DataTable.DataColumns非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文