Color row in DataGrid (WPF)

So you want to change row background color in your datagrid? 

You can do it easily in datagrid loadingrow event.

Example in C#

private void dataGrid1_LoadingRow(object sender, DataGridRowEventArgs e) {
// Get the DataRow
DataRowView item = e.Row.Item as DataRowView;
     if (item != null) {
                DataRow row = item.Row;
                decimal price = Convert.ToDecimal(row["price"]);
                if (price < 0)
                {
                        e.Row.Background = new SolidColorBrush(Colors.Red);
                }
                else
                {
                        e.Row.Background = new SolidColorBrush(Colors.Green);
                }
       }
}

Comments

Name: 
Cancel
Max
   12/1/2014 1:41:38 PM
Thanks again!