2014-05-02 44 views
0

我有一個datagrid,我試圖改變使用datagrid_LoadingRow事件處理程序的單個行顏色。我遇到的問題是,根據滿足哪種條件,將數據網格中的每行都用相同顏色而不是每行分別着色。Datagrid格式行

這是我的代碼,我如何將這個應用到每個單獨的行?

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); 
      } 
     } 
    } 

感謝您一如既往的幫助。

+0

繪畫事件會更有用。這是一個建議。 – linguini

回答

1

爲每一行調用函數schDataGrid_LoadingRow。 因此,不是循環所有項目,取出行中的項目:

 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); 
     } 
+1

謝謝Bubavanhalen – ullevi83