Hi,
I am performing math calculations on grid. Some columns have negative numbers from the time the grid loads and some column values keep changing when the cell value in other column changes.
I need to show the negative numbers in red. Right now I have written the code within the initialize row event and also within cell change event to show the negative numbers in red. It is performing fine sometimes and not showing well sometimes.
Is there any other better way to carry out this one??
The way you are doing it sounds correct.
What's the problem? What does "not showing well sometimes" mean?
This is the code I used in Initialize Row event of the grid and also in cell change event
If CDec(e.Row.Cells(i).Value.ToString) < 0.0 Then e.Row.Cells(i).Appearance = NegativeAppearance Else e.Row.Cells(i).Appearance.ForeColor = Color.Black End If
NegativeAppearance is an appearance object where forecolor and forecolordisabled color set to red.
This works fine when the form is showing values for the first time but when I change some data and some field values become negative from positive then it is not showing me the modified one in red.
This code will not work correctly because in one case you are setting the Appearance property and in the other you are setting a property on the Appearance.
Think about it like this... Suppose you have a 10 rows in the grid and the value of every row is negative. So your InitializeRow code fires and you set the appearance on each cell to your NegativeAppearance.
e.Row.Cells(i).Appearance = NegativeAppearance
Now you change the value of a Cell from negative to positive.InitializeRow fires again and this code gets called:
e.Row.Cells(i).Appearance.ForeColor = Color.Black
e.Row.Cells(i).Appearance has already been set to NegativeAppearance. So this line of code is exactly the same as this:
NegativeAppearance.ForeColor = Color.Black
So all cells in the grid that are using the NegativeAppearance will now be Black instead of Red.
What you need to do is create a PositiveAppearance and assign that appearance to cells you want to be black and assign that appearance to the appropriate cells instead of doing two different things.