Hi,
Within a WinForms UlraGrid, I am looking for a means (via code) to alter the appearance of an entire row, whenever a targeted column value meets a certain condition. For example, I already know that I can target the respect cell via the ConditionValueAppearance. The closest I could find to what might work is the ConditionAppearanceRow Class. Unfortunately you don't provide any code snippets to demonstrate how this class is hooked up with a given DisplayLayout Band.
In any case, I need this capability through code, so I can set the row appearance on the fly. If there is another way to do this, I am all ears.
Thanks,
Lacy
Hi Kelly,
I would probably recommend creating the appearance as part of the grid.DisplayLayout.Appearances collection. Which means you create the appearances for each grid.
You can try creating the appearance outside the grid and sharing it amongst all of the grids, but I think there might be a problem with that because of the ImageList. The Appearance needs the grid to get to the grid's ImageList in order to get images from an ImageList. Even if these are not being used, it might cause an error if you try to use the same Appearance object in more than one grid. You can try it out, though. If there is a problem, you will get an exception when the grid's try to paint. If there's no exception and both grids display okay, then I would say it's probably safe to use the same appearance for all grids.
Hi Mike,
Do I have to create the three appearances (red, yellow, green) in the InitializeLayout event for each of the grids and combos I want to use it on, or is there a way to make global appearances that I can reference?
Thanks,~Kelly
Thanks Mike!
This is very helpful, detailed information. I did not know about the creating of cells just by assigning a backcolor, etc - this will help a lot!
Thanks again!~Kelly
Well, one inefficient thing I notice here is that by using the enumerator on the Cells collection, you are forcing the creation of every cell in the grid. You should try to avoid creating cells you don't need.You can improve the performance a little and the memory usage a lot, but looping through the columns instead of the cells.
You can do something like RowName.Band.Columns to get the columns collection.
You could do the same check you are already doing on the column key. But when you want to check the Value of the cell, use RowName.GetCellValue and pass in the column. This allows you to get the cell value without creating the cell object.
Now... when you apply an Appearance to the cell, you have to create the cell, anyway. So the above will not be very helpful until you also eliminate some cells. Since you are applying one of three colors to every cell, you may want to set the CellAppearance on the column to the color that is most commonly used. That way you only have to create cells when using one of the other two colors.
Also, by referencing the MyCell.Appearance.BackColor, you are creating an Appearance object for every cell. This is inefficient, becuase you will have lots of duplicate appearance objects you don't really need. It would be better to create 3 Appearance objects (one for each color) and then assign the entire Appearance to the cell.
Hi Mike, here is my code for the InitializeRow event:
Private Sub UltraGrid_InitializeRow() AddConditionalFormatting(e.Row)End SubPrivate Sub AddConditionalFormatting(ByRef RowName As UltraGridRow) For Each MyCell As UltraGridCell In RowName.Cells If IsNumeric(MyCell.Column.Key) Or MyCell.Column.Key = "Total FTE" Then ' months and total column get formatted If (Not MyCell.Value Is DBNull.Value) AndAlso (MyCell.Value <= 0) Then If MyCell.Value < 0 Then MyCell.Appearance.BackColor = Me.LightRed Else MyCell.Appearance.BackColor = Me.LightYellow End If Else ' value > 0 MyCell.Appearance.BackColor = Me.LightGreen End If End If NextEnd Sub
Thanks Mike, ~Kelly