Hi,
I am getting an error of "Key already exists" when trying to reuse an appearance object with the UltraGrid. Here is the scenario:
1. In the grid's Initialize_Layout method I have the following code to set up my appearance:
// cancel date appearance
if (!this.ugCommon.DisplayLayout.Appearances.Exists(CANCEL_DATE))
{
Infragistics.Win.Appearance cancelDateAppearance = this.ugCommon.DisplayLayout.Appearances.Add(CANCEL_DATE);
cancelDateAppearance.ForeColor = Color.Red;
}
2. My grid has a column called "Cancel Date". If today's date is greater than this value then I need to make the text of the entire row red. To do this I added some code to the InitializeRow event. I compare the dates and if I have to add the red coloring I am doing this:
foreach (UltraGridCell c in e.Row.Cells)
c.SelectedAppearance = this.ugCommon.DisplayLayout.Appearances[CANCEL_DATE];
c.Appearance = this.ugCommon.DisplayLayout.Appearances[CANCEL_DATE]; <== Problem Line of Code
The error occurs on the second line in the foreach loop:
c.Appearance = this.ugCommon.DisplayLayout.Appearances[CANCEL_DATE];
I am not sure what is going on here. I have to make sure both the appearance and the selected appearance have red text, so I need to change both.
I still have not figured out the issue, but I think I am going to have another problem once this is resolved. I can't figure out how to "base" a new appearance off of an existing one. I want the grid's surrent SelectedAppearance and appearance for the cells to remain the same and just change the color of the text to red. When I create a new appearance and change the ForeColor to red I get strange results. I am guessing because the other appearance settings are lost on this new one.
Is there any way to access the app stylist information in code, or another way to base a new appearance on an existing one?
I don't see any way that the line of code you have here could possibly cause a "Key Already Exists" error message. That message indicates that you are attempting to add an item to a collection which already contains an item with the same key. But your code is not adding anything to a collection. you are just setting a property and indexing into an existing collection. Are you sure that's the right line of code? can you post the entire call stack of the error?
In any case, why are you assigning the appearance to each individual cell in the row? It would be a lot more efficient and a lot easier to simply assign the appearance to row, instead.
sw06226 said:When I create a new appearance and change the ForeColor to red I get strange results. I am guessing because the other appearance settings are lost on this new one.
I don't understand what you mean by this. What "strange results" are you getting?
There's no inheritance for Appearances, so you cannot base one appearance on another. But appearances are resolved in order of most specific to least specific, so generally you would only set some properties on the Appearance and the other settings come from somewhere else - ether another less specific appearance or an ultimate default value.