Setting background colour for a row in a grid

Hi all.

I’m using a grid to display mechanical testing requirements and test results. I would like to be able to set the background colour of individual rows … when not the active row … depending on the row state. For example, a shade of red for FAILED tests, and possibly green for OK results.

I’ve been having a look at available grid row settings using intellisense, but haven’t spotted anything obvious relating to colour control. Can anyone offer advice? I use the following code … provided through an earlier forum post … to set focus to the cell where data entry is required.

row = DirectCast(grid.GridControl.DataGridObj.GetContainerFromIndex(1), Xceed.Wpf.DataGrid.DataRow)|

Can row background colour be specified using this approach … or is there a more obvious method :slight_smile:
Any advice will be appreciated.

Greg Shearer

Any advice on this would still be appreciated. I know this must be possible, but I have had no luck at all provided from Google or Intellisense within Factory Studio.

Greg Shearer

I’ve stumbled on a basic method through the documentation covering dynamic cell colours in reports :slight_smile: Now I just need to adapt it to my specific requirements.

As hoped … it wasn’t hard … just not immediately obvious …

index = grid.GridControl.DataGridObj.SelectedIndex + 1
row = DirectCast(grid.GridControl.DataGridObj.GetContainerFromIndex(index), Xceed.Wpf.DataGrid.DataRow)
row.Background = Brushes.Blue

Turns out that this method works … but doesn’t suit my application. The application will generally have a set of ‘shown’ rows which is greater than the number or rows visible within the grid.

I need to be able to set the colour of the row even if it is off the bottom of the visible grid, but this method appears to blow up if the index used is outside the visible range.

I’m after a simple method to specify the background colour for any grid ‘shown’ row, and also to return it to the original colour if/when the condition is reversed.

How hard can it be :slight_smile:
Any advice will be really appreciated!

Greg Shearer

I am making some progress … but it is slow.

Can anyone tell me how to determine the default row background colours for the alternating grid rows.
I can do this … rather unreliably … by saving the background colours of the top two grid rows on a nominal first pass of the grid.

I think all my problems may be solved if I can simply obtain these colours from a system function.

I want to be able to set the data grid default row background colours like this …


but there don’t appear to be properties available within the TDataGridWindow class …

I came up with an adequate solution.

Obtained the current standard row color HEX expressions using debug techniques.
These translate to RGB values of 255, 255, 255 and 237, 237, 237.
Then …

Dim odd_color as Color
Dim even_color as Color
Dim odd_brush as Brush
Dim even_brush as Brush

odd_color = Color.FromRgb(255, 255, 255)
odd_brush = New SolidColorBrush(odd_color)
even_color = Color.FromRgb(237, 237, 237)
even_brush = New SolidColorBrush(even_color)

So now I can reset odd and even rows to their original color/brush when/if the changed color is no longer required by using something like …

if index Mod 2 Then
	row.Background = odd_brush
	row.Background = even_brush
End if

This seems to work a treat :slight_smile:

We’re glad to hear that you have progressed and found a solution to your problem! As an alternative, you can use the code below to set the background colors of the rows in your grid:

public class ValueToBrushConverter : IValueConverter
//TDataGridWindow grid;
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    	Xceed.Wpf.DataGrid.DataRow row = value as Xceed.Wpf.DataGrid.DataRow;
    	int index = row.TabIndex;
    			return Brushes.Blue;
    			return Brushes.Red;
    	//return cell.ParentRow.IsSelected ? Brushes.Aqua : Brushes.White;
    	//return cell.ParentRow.IsMouseCaptured ? Brushes.Aqua : Brushes.White;
        //return Brushes.Blue;

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        throw new NotSupportedException();

public void DisplayOpening()

public void DisplayIsOpen()
	// Add your code here

public void DisplayClosing()
	// Add your code here

//This method is only called on Dialog displays
public bool DialogOnOK()
	// Add your code here

	return true;

private void SetBinding() 
	Style styleDataRow = new Style(typeof(Xceed.Wpf.DataGrid.DataRow));
    Binding binding = new Binding();
    binding.RelativeSource = RelativeSource.Self;
    binding.Converter = new ValueToBrushConverter();
    styleDataRow.Setters.Add(new Setter(Control.BackgroundProperty, binding));
    TDataGridWindow grid = CurrentDisplay.GetDataGrid("Grid"); 
  	grid.GridControl.DataGridObj.Resources[typeof(Xceed.Wpf.DataGrid.DataRow)] = styleDataRow;

We hope this is helpful for you!

Best Regards,
Tatsoft Team.

Hi Bruno.

Thanks for your response … but I think I really just need to be able to set the default row colours for the grid … if possible before it is populated. I can then simply set and reset row backgrounds as row status changes.

I have also found a limitation of using the Xceed.Wpf.DataGrid.DataRow method within my application, in that it is only applicable for DataGrid rows currently within the display window. Ideally I would like to set the background colours for rows whether they currently within the display window or not.

For example, I can have a data grid containing 40 rows, with only 20 visible at any given time. However, if I try to set the background colour within a coding loop, for a row which is not currently visible, the code execution fails.

At this stage, if I can simply set the data grid default background colours, I think I can manage the rest. Is there a simple method available to do this?

Hi again Bruno.

I think you can ignore my note above, as I’ve convinced myself that the method I was using simply won’t work for what I am trying to achieve.

So I thought I’d better further investigate the solution you provided. The initial problem I’m having is that all of my screen coding is in VB .NET, so I have used the built in code conversion to convert from C#. When I do this, the code appears to convert OK, but when compiled gives this error:

I am not yet proficient with .NET, and don’t understand what needs to be done to fix the problem. Can you please point me in the right direction.