Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 数据网格格式化行_C#_Wpf_Datagrid - Fatal编程技术网

C# 数据网格格式化行

C# 数据网格格式化行,c#,wpf,datagrid,C#,Wpf,Datagrid,我有一个datagrid,我正试图使用datagrid_LoadingRow事件处理程序更改其上各个行的颜色。我遇到的问题是,它将数据网格中的每一行着色为相同的颜色,而不是每一行,这取决于满足的条件 这是我的代码,如何将其应用于每一行 private void schDataGrid_LoadingRow(object sender, DataGridRowEventArgs e) { foreach (DataRowView dr in schDataGrid.

我有一个datagrid,我正试图使用datagrid_LoadingRow事件处理程序更改其上各个行的颜色。我遇到的问题是,它将数据网格中的每一行着色为相同的颜色,而不是每一行,这取决于满足的条件

这是我的代码,如何将其应用于每一行

    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);
            }
        }
    }
private void schDataGrid_LoadingRow(对象发送方,DataGridRowEventArgs e)
{
foreach(schDataGrid.Items中的DataRowView dr)
{
字符串DUEDATE=dr[“DUEDATE”].ToString();
DateTime now=Convert.ToDateTime(DateTime.now.ToString(“dd/MM/yyyy”);
DateTime compareDate=Convert.ToDateTime(DUEDATE);
时间跨度差异=现在-比较;
如果(差异0天和差异60天)
{
e、 Row.Background=新的SolidColorBrush(Colors.Red);
e、 Row.Foreground=新的SolidColorBrush(Colors.White);
}
}
}

感谢您一如既往的帮助。

每一行都会调用函数schDataGrid\u 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);
        }
var dr=e.Row.Item作为您的项目
//其他东西。。。。
如果(差异0天和差异60天)
{
e、 Row.Background=新的SolidColorBrush(Colors.Red);
e、 Row.Foreground=新的SolidColorBrush(Colors.White);
}

Paint事件会更有用。这是一个建议。