I am using 2009.1 w/the latest hotfixes.
I need loop through all my rows and find cells that match a value or a color. At first I was doing ..
foreach (UltraGridRow aRow in gridConstraints.Rows){ foreach(UltraGridCell aCell in aRow.Cells) { if(aCell.Appearance.BackColor == Color.Gray) { //do stuff } } }
Each call to the the if statement cuased the memory to jump like crazy, after about 100 rows I am up to 1-1.5GB of memory usage. So I thought maybe the Color.Gray was allocating some memory everytime so I thought I would try comparing a value. The same problem happens...so I thought I would try this with LINQ like this..
foreach (UltraGridRow aRow in gridConstraints.Rows){
List<UltraGridCell> cells = (from UltraGridCell c in aRow.Cells where c.Value == "X" select c).ToList();
foreach (UltraGridCell aCell in cells) { } cells.Clear(); cells = null;}
...hoping the .Clear() and setting the list to null would clear the used RAM, but it is still using the same amount.
So my questions are why does doing a compare cause the memory to increase? How can I do this w/o the memory increasing or how can I release the used memory after each compare?
Yes, that's a much more efficient way to do it.
For future reference, if you have to loop through the grid and examine the values of the cell, using the cell's BackColor is a very inefficient way to do it. You'd be much better off using the row's GetCellValue method which will allow you to get the value of the cell without creating a cell object or an Appearance.
Never mind...I'm an idiot. I can just store what has been clicked in the MouseDown event...duh.
Basically I just need all of the cells that match some value. I have a grid where the users by clicking cells designate some constraints. I change the cell color to gray to indicate the ones they have clicked. I then save these constraints to a DB so i need to get all of the cells they clicked. So i need some method to find all of those cells w/o using gobs of RAM and causing a crash.
The grid creates the cell objects for a row lazily - that is... the UltraGridCell is not created unless someone tries to access it. Row objects are the same way.
So by looping in this way, you are forcing the creation of an UltraGrid and UltraGridCell object for every row and cell in the grid. If you have a lot of rows/cells, then this can quickly add up to a lot of memory. Although, you must have quite a lot to reach 1.5GB.
It's hard to give you any specific advice about how to achieve this more efficiently, since I don't really know what you are trying to do. But you should check out the WinGrid Performance Guide.