Click here to Skip to main content
15,896,439 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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);
Posted
Updated 31-Mar-11 0:16am
v5

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!
 
Share this answer
 
Comments
willempipi 31-Mar-11 6:09am    
Why did you add this as a solution?

DateTime / Stopwatch class ===> output is the same.
BobJanova 31-Mar-11 6:14am    
DateTime is accurate to about 15ms, easily good enough for this kind of thing.
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; 

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900