We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Individual cell style setting

Hi, I am evaluating the Essential Grid. I am trying to set the format of a cell based on the value contained within the cell. The PrepareViewStyleInfo event works well for this; unfortunately, the code I am running to evaluate the condition is quite slow and as a consequence, the grid cursor movements are slow. For our application, the style of the cells only needs to be re-evaluted when new data is loaded and so I would like to set the style only at that time (I guess it would also be necessary to evaluate on column moves, sorting etc). Is this possible, or can you suggest a better method? The code looks something like: private void grid1_PrepareViewStyleInfo(object sender, GridPrepareViewStyleInfoEventArgs e) { double rejectMax = getRejectMax(); // this takes too long for smooth cursor movement double rejectMin = getRejectMin(); double val = (double)e.Style.CellValue; if ( val < rejectMin) e.Style.BackColor = Color.Red; else if (val > rejectMax) e.Style.BackColor = Color.Yellow; } }

7 Replies

CB Clay Burch Syncfusion Team September 3, 2002 05:48 PM UTC

> Hi, I am evaluating the Essential Grid. I am trying to set the format of a cell based on the value contained within the cell. The PrepareViewStyleInfo event works well for this; unfortunately, the code I am running to evaluate the condition is quite slow and as a consequence, the grid cursor movements are slow. > For our application, the style of the cells only needs to be re-evaluted when new data is loaded and so I would like to set the style only at that time (I guess it would also be necessary to evaluate on column moves, sorting etc). Is this possible, or can you suggest a better method? > The code looks something like: > private void grid1_PrepareViewStyleInfo(object sender, GridPrepareViewStyleInfoEventArgs e) > { > double rejectMax = getRejectMax(); // this takes too long for smooth cursor movement > double rejectMin = getRejectMin(); > double val = (double)e.Style.CellValue; > if ( val < rejectMin) > e.Style.BackColor = Color.Red; > else if (val > rejectMax) > e.Style.BackColor = Color.Yellow; > } > } > Before trying to respond, can you provide some more information? Are you using a GridControl or a GridDataBoundGrid? If it is a GridControl, are you populating the grid with this data, or are you using the grid in a virtual manner, handling QueryCellInfo? I suspect that if getRjectMin takes a long time, you will want to actually populate a GridControl with your data (& backcolor), and then only call getRejectMin when the data changes, not everytime it is displayed.


AK Andre King September 3, 2002 06:39 PM UTC

Hi, Thanks for the quick reply. Sorry, I forgot to say I am using the databoundgrid and am setting the datasource to a datatable to populate the grid. Thanks.


CB Clay Burch Syncfusion Team September 4, 2002 06:23 AM UTC

For a GridDataBoundGrid, there is no individual cell information store by the control. This is why events like PrepareViewStyleInfo (or QueryCellInfo) are used to provide the data on demand. So, in a GridDataBoundGrid, the only way to color a cell is through an event. In this case, you would have to either speed up the processing in the getRejectMax and getRejectMin methods, or maybe somehow store these values and just retrieve them in your event handler instead of calculating them. If the values from GetRejectMax and getRejectMin are not so volatile that they have to be computed at the instance of a cell being displayed, then precomputing them may be an option. If they have to be computed at the instance of display, then speeding up how they are computed is the only answer.


AK Andre King September 4, 2002 11:41 AM UTC

Thanks for your reply. I'll probably end up caching the values as you suggest.


CB Clay Burch Syncfusion Team September 4, 2002 04:33 PM UTC

Just something to try... I don't think it will make a noticeable difference. Move your code from PrepareViewStyleInfo into QueryCellInfo. QueryCellInfo is hit only once per draw (and its values cached temporarily) whereas PrepareViewStyleInfo may be hit more often as it does not cache anything.


AK Andre King September 4, 2002 09:58 PM UTC

Thanks for the suggestion. I moved the code to the QueryCellInfo event but got some strange behaviour. One thing that is different is the method I was using to get the header: grid[0,e.ColIndex].CellValue (is this the best way to get the header text?) gives me an empty string now. The coloring of the cells is erratic and changes as I move around in the grid. The speed does seem a bit better, but I think I will try optimizing the lookup and use the other method.


CB Clay Burch Syncfusion Team September 5, 2002 06:29 AM UTC

Normally, that would be the correct way to get the header text. But in QueryCellInfo, you have to be careful as trying to access the values from other grid cells result in another call to QueryCellInfo. Without taking care, you can set up recursive calls that result in infinite loops or other problems.

Loader.
Live Chat Icon For mobile
Up arrow icon