3

I'm using FormatConditions to format certain rows based on their properties. E.g. I have a gridcontrol with many rows and some rows are colored red when there is an error. The issue I have is that when a row is selected, the formatting disappears and goes blue.

I would like the selected row color to be a slightly darker shade of whatever color that row is. I.e. You can still tell what color the row was shaded to be, but you can also tell that it's selected.

What's the best way to do this?

user832747
  • 167
  • 2
  • 4
  • 14

2 Answers2

2

Handle GridView.CustomDrawCell Event and Get the state of current Cell, whehter it belong to selected row or not.

//Get State of the cell - e.Cell provide access to current cell to paint-

 GridRowCellState state = ((GridCellInfo)e.Cell).State;

Then check for the selected row cell

if ((state & GridRowCellState.Selected) == GridRowCellState.Selected)
{
// do your custrom drawing here.
// for example 
e.DisplayText = "";
e.Appearance.BorderColor = Color.White;
}

Then set the CustomDrawEventArgs.Handled Property - e.Handled = true or false; after custom painting the cell as per your requirment.

Go Through these documentation links:
Custom Painting Basics
Custom Painting Samples

Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75
  • Ok, so I can change the color of the cells, but how can I get the value specified by my FormatConditions? I have tried using an if condition on e.Appearance.BackColor, but it doesn't look like it's formatted the cell with the color yet. I.e. I want a selected row to be DarkRed if the unselected row is formatted to be normal Red. – user832747 Feb 23 '12 at 06:59
  • You can still tell what color the row was shaded to be... regarding this.. `e.Cache` give you the border, background etc pen and brush object that will help you get the color etc.. you can get the formatconditions as: `GridView view = (GridView)sender; StyleFormatCondition condition1 = view.FormatConditions["nameofcondition"];` – Niranjan Singh Feb 23 '12 at 07:12
1

I believe you would need to do this manually by handling the GridView.CustomDrawCell event and set the row colour based on the current row state. If memory serves me correctly, you should check the RowCellCustomDrawEventArgs.Cell property for row state information.

From your question you only need to modify the behaviour when the row is selected so you can simply skip the custom draw (set e.Handled=False) if the row is not selected and your existing functionality will continue unchanged.

Kuffs
  • 35,581
  • 10
  • 79
  • 92
  • Ok, so I can change the color of the cells, but how can I get the value specified by my FormatConditions? I have tried using an if condition on e.Appearance.BackColor, but it doesn't look like it's formatted the cell with the color yet. I.e. I want a selected row to be DarkRed if the unselected row is formatted to be normal Red. – user832747 Feb 23 '12 at 07:02