Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
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:

C#
// _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>


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?
Posted
Updated 25-Mar-11 1:08am
v3
Comments
willempipi 25-Mar-11 11:16am    
Nobody with a solution?

1 solution

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!
 
Share this answer
 
v2
Comments
willempipi 25-Mar-11 7:33am    
Tried it, but didn't solve the speed problem...
E.F. Nijboer 25-Mar-11 8:43am    
Another thing is this line: foreach (FilterColumnClass filter in _FilterColumns)
You do not break when the item is found. So if the first element is the one you are loking for, you still go trough all of the items. You could simply add Break; when the item is found or use the code I put into my answer.
willempipi 25-Mar-11 9:50am    
I also tried your new code but it doesn't help, it still takes around 250 ms to load 89 columns.

I don't really get your suspend/resumelayout solution, as it is a normal in-memory DataTable object that i use to read the columns.

The problem lies with the "foreach (DataColumn col in _AllData.Columns)" code which is just plain slow...
E.F. Nijboer 25-Mar-11 15:34pm    
Making changes or creating/deleting UI elements can slow the application down. The initialization code that the ide creates itself does this as well to avoid that. (http://msdn.microsoft.com/en-us/library/system.windows.forms.control.suspendlayout.aspx) But in your case this isn't the issue apparently. You could also try the following:
<dataset>.EnforceConstraints = false
// Perform some operations on the dataset
<dataset>.EnforceConstraints = true;

You could also try calling BeginEdit/EndEdit of the dataset.
More info: http://msdn.microsoft.com/en-us/library/s3bxwk8b%28v=vs.80%29.aspx

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