Changing Datagrid Cell/Row background color in code behind

May 5, 2009 at 11:22 AM
I been reading various tutorials and articles on how to change a cell's background color using data triggers in the xmal file but I'm guessing in my situation it may need to be done in code behind.

Basically what I need is my datagrid to display using the following logic

First the column names:
MRP, STATUS, ORDER, INCORDER

The 4 cell styles:
1) Grey background with border
2) Yellow background with border
3) White background with border
4) White background with no border

Logic:
If "STATUS" has a value of "N" then set each cell style to 2 except the cell in first column.
Else If "STATUS" has a value of "C" then set cell style to 1 except the first cell in first column. Also ignore, (don't display), values in cells "ORDER" and "INCORDER".
Else If "INCORDER" has the value of "Y" then set cell style in "ORDER" to 2.
Else If cell has a value then set the cell style to 3 otherwise set the cell style to 4.

How could I achieve the above?

Thanks in advance
May 6, 2009 at 4:09 PM
Problem Solved!
May 7, 2009 at 9:07 PM
Could you post your solution?

Thanks!
May 8, 2009 at 11:05 AM
Edited May 8, 2009 at 11:17 AM

ok well first the xmal and then the code behind. 

<Window.Resources>

<MRP_Report_View:CellColorConverter x:Key="myCellColorConverter"/> 

<MRP_Report_View:CellBorderConverter x:Key="myCellBorderConverter"/> 

</Window.Resources>

Now I add a new cell style to the datagrid which will use the converters to get both cell background color and cell borders.

<Custom:DataGrid.CellStyle>

<Style TargetType="{x:Type Custom:DataGridCell}">

<Style.Triggers>

<Trigger Property="IsSelected" Value="True">

<Setter Property="Background" Value="#3399FF"/>

</Trigger>

</Style.Triggers>

<Setter Property="Background">

<Setter.Value>

<MultiBinding Converter="{StaticResource myCellColorConverter}">

<MultiBinding.Bindings>

<Binding RelativeSource="{RelativeSource Self}"/>

<Binding Path="Row"/>

</MultiBinding.Bindings>

</MultiBinding>

</Setter.Value>

</Setter>

<Setter Property="BorderBrush">

<Setter.Value>

<MultiBinding Converter="{StaticResource myCellBorderConverter}">

<MultiBinding.Bindings>

<Binding RelativeSource="{RelativeSource Self}"/>

<Binding Path="Row"/>

</MultiBinding.Bindings>

</MultiBinding>

</Setter.Value>

</Setter>

</Style>

</Custom:DataGrid.CellStyle>

Thats it as far as the xmal goes. Now for the converters in code behind.

First the cell background converter. Here we have to use the IMultiValueConverter. This allows us to get information on the cell, its column name and the datarow it belongs too. Using this you can set the cell color based on things like the column name, the cell value or the value of another cell in the same row. For more information on IMultiValueonverter check the following. http://msdn.microsoft.com/en-us/library/system.windows.data.imultivalueconverter.aspx . For information on the convert method check here. http://msdn.microsoft.com/en-us/library/system.windows.data.imultivalueconverter.convert.aspx

public

{

 {

// The second object in values should be the datarow the cell belongs too. The first object should be the cell itself.

if (values[1] is DataRow)

{

var cell = (DataGridCell)values[0];

 

var columnName = cell.Column.SortMemberPath;

var row = (DataRow)values[1];

//* Change the background color of cell depending on the following rules *//

// Check if cell is not in the first column (MRP). If it is then do nothing.

// Check to see if the cell in column STATUS has the value of NEW. If it does then paint the cell Yellow

// If not check Check to see if the cell in column STATUS has the value of COMP. If it does then paint the cell LightGray

// If not, Check to see if the current cell is in column DELQ. If it is then check to see if the cell in column DELQINC has the value 'True' If it does then paint the cell Yellow

// If not, Check to see if the current cell is in column NEXT. If it is then check to see if the cell in column NEXTINC has the value 'True' If it does then paint the cell Yellow

// In none of the above then the cell will be default color

 

if (columnName != "MRP")

{

if (row["STATUS"].GetType() != typeof(System.DBNull) && (string)row["STATUS"] == "NEW")

return new SolidColorBrush(Color.FromArgb(255,255,255,123)); // Yellow

else if (row["STATUS"].GetType() != typeof(System.DBNull)&& (string)row["STATUS"] == "COMP")

return new SolidColorBrush(Colors.LightGray);

else if (columnName == "DELQ" && row["DELQINC"].GetType() != typeof(System.DBNull) && (string)row["DELQINC"] == "True")

 return new SolidColorBrush(Color.FromArgb(255, 255, 255, 123)); // Yellow

else if (columnName == "NEXT" && row["NEXTINC"].GetType() != typeof(System.DBNull) && (string)row["NEXTINC"] == "True")

return new SolidColorBrush(Color.FromArgb(255, 255, 255, 123)); // Yellow

}

return SystemColors.AppWorkspaceColor;

}

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)  

{

throw new System.NotImplementedException();

}

I haven't posted the code for the cell border converter as i think you get the idea. If you would like to see it then let me know and I will post it. Also if you need a better explanation as to when the above is doing or if there is something missing or you have other questions then just me know and I will see if I can help :-D