Mam datagrid, że próbuję zmienićposzczególne kolory wierszy na temat korzystania z obsługi zdarzeń datagrid_LoadingRow. Problemem jest to, że koloruje każdy wiersz w datagrid tego samego koloru, zamiast każdego wiersza w zależności od tego, który warunek jest spełniony.
Oto mój kod, jak zastosować to do każdego osobnego wiersza?
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);
}
}
}
Dziękuję za pomoc jak zawsze.
Odpowiedzi:
1 dla odpowiedzi № 1Funkcja schDataGrid_LoadingRow jest wywoływana dla każdego pojedynczego wiersza. Więc zamiast zapętlać wszystkie swoje przedmioty, usuń pozycję z wiersza:
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);
}