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

GridControl Fastest Way to Set TextColor To Red if Value < 0

Hi, I have a user control that derives from GridControl. I''m overridding OnQueryCellInfo in order to set a cell''s TextColor to red if the cell''s value is less than 0 But this now runs very very slowly. Is there a preferred/faster way to achieve this? Here''s my current code: protected override void OnQueryCellInfo(GridQueryCellInfoEventArgs e) { base.OnQueryCellInfo(e); try { // For some reason E.Style.CellValue is null!!! if (e.Style.CellType != "Header") { string cellValue = (string)base[e.RowIndex, e.ColIndex].CellValue; if (cellValue != null && cellValue.Length > 0) { double parsedValue = Double.Parse(cellValue); if (parsedValue < 0) { e.Style.TextColor = Color.Red; } } } } catch{} } Help would be very much appreciated Thanks Jason

1 Reply

AD Administrator Syncfusion Team May 13, 2005 11:17 AM UTC

Using QueryCellInfo is the proper place, but always calling Double.Parse(cellValue) and trying to catch an exception when it fails is not the quickest way to handle this. Instead use double.TryParse. This will test for a valid value without raising an exception and not raising an exception is generally significantly quicker than rasing one.
double d;
string text = grid[row,col].Text;
if(double.TryParse(text, System.Globalization.NumberStyles.Any, null, out d))
     //d will have the double value


Live Chat Icon For mobile
Up arrow icon