Hi
I have been looking at the WinGrid Performance guide and trying to implement some of the suggestions, particularly with regard to re-using appearances. In a grid layout event I set up a red forecolor appearance as follows:
private void ug_InitializeLayout(object sender, InitializeLayoutEventArgs e){ // Set ForColor appearances for use later. if (e.Layout.Appearances.Exists("ForeColorRed") == false) { Infragistics.Win.Appearance redForeColour = e.Layout.Appearances.Add("ForeColorRed"); redForeColour.ForeColor = Color.Red; } // ....}
In the initialize row event I test for a condition and apply font forecolor as follows:
private void ug_InitializeRow(object sender, InitializeRowEventArgs e){ UltraGrid ug = sender as UltraGrid; UltraGridColumn structureNumberColumn = e.Row.Band.Columns["STRUCTURENUMBER"]; int structureNumberValue = Convert.ToInt32(e.Row.GetCellValue(structureNumberColumn)); if (structureNumberValue < 10) { e.Row.Appearance = ug.DisplayLayout.Appearances["ForeColorRed"]; } else { e.Row.Appearance.ResetForeColor(); } // ...}
In this simplified case I want to set the row forecolor to red if the number value is < 10, otherwise use the default color. However, using ResetForeColor() appears to reset the forecolor for all rows in the grid (it's as if the saved appearance is destroyed the first time ResetForeColor() is called).
I can set another saved appearance such as 'ForeColorBlack' and that works fine but I just wanted to know if you can see anything wrong with my original approach because I can't figure out quite what's happening.
Regards
Adrian
Apologies, I should have been more specific about this. All is fine using ResetForeColor() in InitializeRow when the grid is first loaded. The problem occurs when InitializeRow fires after a cell has been edited; if the condition results in ResetForeColor() being called then the fore color appearance for all rows is reset, not just the one changed.
Hope that makes more sense.
Hi Adrian,
When you set the Appearance property on a cell, the Appearance property uses that instance of the Appearance object. If you set the same appearance on multiple cells, then every cell you applied it to is using the same instance of the Appearance.
So any changes you make to the Appearance will be applied to all of those cell. This is actually a very powerful too. If your users decide they want to use green instead of red, all you would have to do is change the color on the appearance.
Anyway... to reset the cell back to it's default state, don't Reset the ForeColor on the Appearance. What you need to do is tell the cell not to use that Appearance object any more. So you can either call cell.ResetAppearance or set the cell.Appearance = null.