Reputation: 199
I have a datagrid that I am trying to change the individual row colours on using the datagrid_LoadingRow event handler. The problem I have is, it is colouring every row in the datagrid the same colour instead of each individual row depending on which condition is met.
Here is my code, how do I apply this to each separate row?
private void schDataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
foreach (DataRowView dr in schDataGrid.Items)
{
string DUEDATE = dr["DUEDATE"].ToString();
DateTime now = Convert.ToDateTime(DateTime.Now.ToString("dd/MM/yyyy"));
DateTime compareDate = Convert.ToDateTime(DUEDATE);
TimeSpan difference = now - compareDate;
if (difference.Days <= 0)
{
e.Row.Background = new SolidColorBrush(Colors.ForestGreen);
e.Row.Foreground = new SolidColorBrush(Colors.White);
}
else if (difference.Days > 0 && difference.Days <= 60)
{
e.Row.Background = new SolidColorBrush(Colors.Orange);
e.Row.Foreground = new SolidColorBrush(Colors.Black);
}
else if (difference.Days > 60)
{
e.Row.Background = new SolidColorBrush(Colors.Red);
e.Row.Foreground = new SolidColorBrush(Colors.White);
}
}
}
Thanks for your help as always.
Upvotes: 0
Views: 149
Reputation: 148
The function schDataGrid_LoadingRow is called for every single row. So instead of looping all your items, take out the item of the row:
var dr = e.Row.Item as yourItem
// Other stuff....
if (difference.Days <= 0)
{
e.Row.Background = new SolidColorBrush(Colors.ForestGreen);
e.Row.Foreground = new SolidColorBrush(Colors.White);
}
else if (difference.Days > 0 && difference.Days <= 60)
{
e.Row.Background = new SolidColorBrush(Colors.Orange);
e.Row.Foreground = new SolidColorBrush(Colors.Black);
}
else if (difference.Days > 60)
{
e.Row.Background = new SolidColorBrush(Colors.Red);
e.Row.Foreground = new SolidColorBrush(Colors.White);
}
Upvotes: 1