空数据表的数据绑定速度慢 [英] Slow databinding of a empty datatable

查看:76
本文介绍了空数据表的数据绑定速度慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我的问题最近2次未得到回答,因此我将重试:

我在usercontrol上有2个gridviews.当我将数据表(具有80列/100行的DataTable _AllData)绑定到第一个网格时,它需要10毫秒.但是当我创建一个有1个空行的数据表并将其绑定到第二个网格时,它需要180毫秒,这太长了.

我的代码:

As my question was not answered the last 2 times I''ll try again:

I have 2 gridviews on a usercontrol. When I bind a datatable (DataTable _AllData with 80 columns/ 100 rows) to the first grid it takes 10 ms. But when i create a datatable with 1 empty row and bind it to the second grid it takes 180 ms, which is too long.

My code:

stopWatch = new Stopwatch();
stopWatch.Start();

// Create a datatable for the header with empty strings
DataTable header = new DataTable("FilterRow");

// Add all columns of the datasource to it
foreach (DataColumn col in _AllData.Columns)
{
    header.Columns.Add(new DataColumn(col.ColumnName, typeof(string)));
}

stopWatch.Stop();
TimeSpan s3 = stopWatch.Elapsed; // ===> 0.0966
stopWatch = new Stopwatch();
stopWatch.Start();

// Create a empty row for it
DataRow filterrow = header.NewRow();

foreach (FilterColumnClass filter in _FilterColumns) // While testing the timings this contained 80 columns
{
    if (filter.FilterCheckControl.Visible)
    {
        try
        {
            filterrow.ItemArray[filter.Index] = filter.FilterTextString; // While testing the timings all these variables where empty
        }
        catch (Exception ex)
        {
            PublicFunctions.ReportBug(ex, _Shared);
        }
    }
}

// Add the filter row to the header datatable
header.Rows.Add(filterrow);

stopWatch.Stop();
TimeSpan s4 = stopWatch.Elapsed; // ===> 0.1811 ms
stopWatch = new Stopwatch();
stopWatch.Start();

// Assign the header datatable to the grid
gridHeader.DataSource = header;

stopWatch.Stop();
TimeSpan s5 = stopWatch.Elapsed;  // ===> 165.3965 ms WHY??????

MessageBox.Show(
    "PrepareFilterColumns" + Environment.NewLine +
    "#1-2#: " + s1.TotalMilliseconds.ToString() + Environment.NewLine +
    "#2-3#: " + s2.TotalMilliseconds.ToString() + Environment.NewLine +
    "#3-4#: " + s3.TotalMilliseconds.ToString() + Environment.NewLine +
    "#4-5#: " + s4.TotalMilliseconds.ToString() + Environment.NewLine +
    "#5-6#: " + s5.TotalMilliseconds.ToString() + Environment.NewLine);

推荐答案

不要使用DateTime类进行计时:它的辨别力太差了.

而是使用 Stopwatch类 [
Don''t use the DateTime class for timing: it''s discrimination is way too poor.

Instead, do your timings with the Stopwatch class[^]: it is much more suited to this, and has a lot better resolution. You may find your existing timings are miles out!


我找到了解决方案!!!

将数据表数据绑定到网格的速度如此之慢的主要原因是,列名的绘制花费了大量时间.简单的代码行:
I''VE FOUND THE SOLUTION!!!!

The main reason that databinding a datatable to a grid is so slow is because the drawing of the columnnames takes alot time. The simple line of code:
gridHeader.ColumnHeadersVisible = false; 


将数据绑定过程从 180-300毫秒加快到 4毫秒 !!!

现在这可能不是一个很好的解决方案,因为表列对于显示数据非常重要,但是对我来说这不是问题,因为我已经创建了一些自定义控件,这些控件放置在列名上.因此,通过禁用列名,我自己的column-name-controls仍然存在,并为用户提供了有关哪一列是什么的见识.


Speeds up the databinding process from 180-300 ms to 4 ms!!!

Now this might not be a good solution for you because the tablecolumns are quite important for the showing of data, but for me it''s not a problem as I already create some custom controls that I place over the column names. So by disabling the column names, my own column-name-controls remain present and give the user insight in which column is what.


这篇关于空数据表的数据绑定速度慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆