The purpose of this article is to provide some general programming practice guidelines and troubleshooting tips to improve performance when using the UltraWinGrid control. Not all of the tips provided here will apply to every application, but it should help with the most common performance issues a developer is likely to encounter.
If you are concerned about reducing the amount of memory used by the grid, then there are several important things you can do to reduce it.
The grid does not create UltraGridCell objects for every row of data. Cells are only created as neccessary. So whenever possible, you should avoid accessed a cell. For example, suppose your code is using the InitializeRow event of the grid in order to calculate a total. For example:
}
This code references three cells in each row of the grid, thus creating a lot of objects which are potentially unneccessary. This can be avoided using methods on the row to deal with cell values, rather than the cell objects themselves. Like so:
By using the GetCellValue method, we can eliminate 2 cells per row in this example and save memory.
Another common use for the InitializeRow event is to apply an appearance to a cell based on the Value of that cell. Applying an appearance to a cell requires getting a reference to the UltraGridCell, so that is unavoidable. But you can save memory by re-using the same appearance object for multiple cells. Suppose, for example, that you want to change the ForeColor of a cell to red for negative numbers and black for positive numbers. You might do something like this:
This code will create a cell for every row. That is unavoidable, since the cell must be created in order to have an Appearance. The bigger problem here is that the Appearance property on the cell is lazily created. That means that we are not only creating a cell for each row, but a new Appearance object for each row. And since there are only two possible colors, we end up creating a large number of identical appearance objects.
A better way to do this is to create the Appearances we need up front and then re-use the same appearance wherever it is needed.
Another benefit to this approach is that you can change the appearance everywhere en masse. For example, suppose your users want to use Green instead of Black for positive appearances. You could, at run-time, set the ForeColor of the “Positive” Appearance object, and every cell in the grid that was using that appearance would update automatically.
Hi, Mike
Very much useful information, thanks for posting it.
May I know where I can find this type of documents?
Regards,
Ceaser
The KB09280 thing doesn't seem to have any Microsoft links.
When it says it "loads slowly when binding to a DataSet with many relationships" how many are we talking?
Is this fixed in the .Net Framework 3.5?
I suppose I'll just have to try it...
This was a weird problem that was introduced into the BindingManager just before the release of the DotNet Framework 2.0. As far as I know, this has not been fixed, and was the result of some updates and improvements made to the BindingManager. My guess is it was to help BindingSources work better.
Anyway,it's hard to define exactly when it occurs. It's not just a matter of the number of relationships in the data. It can also occur in other cases where there are empty island or bands or with complex data structures.
If you athink you are experiencing this issue, then the best thing to do is simply to wrap your data source in a BindingSource and that will correct the issue. If it does not help, then something else is wrong.
I don't think a DataView helps. When you bind the grid to a DataSet, you are really binding to a DataView, anyway. So you should use a BindingSource to wrap the DataView.
Of course, if you are not currently experiencing any problems, then you probably don't need to worry about it. The kinds of problems that would occur are severe performance issues and lockups and they are usually prettty obvious and hard to miss.
I am having the grid bind to a DataView. Does this issue with binding to DataSet potentially become a performance problem for my app or does the use of DataView shelter me from it?
Thanks!