如何在条件下更改数据网格的行颜色?这对我来说很重要 [英] How to change row color from datagrid on condition? It is importan for me

查看:85
本文介绍了如何在条件下更改数据网格的行颜色?这对我来说很重要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的加载数据库到数据网格的代码

this my code for load Database to datagrid

var data = from p in dc.ToplantiTalepFormus from c in dc.Onays where 
               (p.Toplanti_Talep_ID == c.Toplanti_Talep_ID &&
               (c.Talep_eden_ID==ID || c.User_ID == ID))
           select new 
           {
               p.Toplanti_Talep_ID, p.Toplanti_Talep_Tarihi, 
               p.Toplanti_Tarihi, p.Toplanti_Saat, 
               p.Toplanti_Konusu, p.Toplanti_Yeri,
               p.Toplanti_Şekli, p.Toplantı_Durumu, 
               c.Toplantı_Onay, c.Yeni_Tarih, 
               c.Yeni_Saat, c.DeğişiklikOnay ,c.User_ID
           };

foreach (var ToplantiTalepFormus in data)
{
    Datagrid.ItemsSource = data.ToList();
}





我的尝试:



但是当用户ID =用户ID或其他我要使行为红色时。请帮助我



What I have tried:

but when User Id is = user ID or somthing else I want to make row is red. Please help me

private void Liste()
{
    ID = Convert.ToInt32(MainWindow.UserID);

    var data = from p in dc.ToplantiTalepFormus from c in dc.Onays where 
        p.Toplanti_Talep_ID == c.Toplanti_Talep_ID select new { p.Toplanti_Talep_ID,
        p.Toplanti_Talep_Tarihi, p.Toplanti_Tarihi, p.Toplanti_Saat,
        p.Toplanti_Konusu, p.Toplanti_Yeri, p.Toplanti_Şekli, p.Toplantı_Durumu,
        c.Toplantı_Onay, c.Yeni_Tarih, c.Yeni_Saat, c.DeğişiklikOnay, c.User_ID };

    foreach (var ToplantiTalepFormus in data)
    {
        if (ToplantiTalepFormus.User_ID == 1)
        {
            Datagrid.RowBackground = Brushes.Red;
        }
        Datagrid.ItemsSource = data.ToList();
    }
}

推荐答案

wpf datagrid conditional row颜色 - Google搜索 [ ^ ] found: wpf - 如何设置DataGrid的行Background,基于使用数据绑定的属性值 - Stack Overflow [ ^ ]



更新:由于您没有使用数据绑定,这是一个代码隐藏版本。

This wpf datagrid conditional row color - Google Search[^] found: wpf - How to set DataGrid's row Background, based on a property value using data bindings - Stack Overflow[^]

UPDATE: As you are not using data binding, here is a code-behind version.
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Mock();
    }

    public ObservableCollection<Person> Persons { get; }
        = new ObservableCollection<Person>();

    private void Mock()
    {
        var rnd = new Random();

        for (int i = 0; i < 100; i++)
        {
            Persons.Add(new Person
            {
                Name =


Person {i}
Age = rnd.Next( 20 50
});
}

DataGrid1.LoadingRow + = DataGrid1_LoadingRow;
DataGrid1.ItemsSource = Persons;
}

private void DataGrid1_LoadingRow( object sender,DataGridRowEventArgs e)
{
var row = e.Row;
var person = row.DataContext as Person;
if (person.Age > 30 && person.Age < 40
{
row.Background = new SolidColorBrush(Colors.Red);
}
}
}

public class Person
{
public string 名称{获得; set ; }
public int 年龄{ get ; set ; }
}
"Person {i}", Age = rnd.Next(20, 50) }); } DataGrid1.LoadingRow += DataGrid1_LoadingRow; DataGrid1.ItemsSource = Persons; } private void DataGrid1_LoadingRow(object sender, DataGridRowEventArgs e) { var row = e.Row; var person = row.DataContext as Person; if (person.Age > 30 && person.Age < 40) { row.Background = new SolidColorBrush(Colors.Red); } } } public class Person { public string Name { get; set; } public int Age { get; set; } }





更新#2:这是一个Hierarchical DataGrid版本... Works同样,只是连接了一点......



UPDATE #2: Here is a Hierarchical DataGrid version... Works the same, just wired up a little different...

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Mock();
    }

    public ObservableCollection<Parent> Parents { get; }
        = new ObservableCollection<Parent>();

    private void Mock()
    {
        var rnd = new Random();

        for (int i = 0; i < 100; i++)
        {
            var parent = new Parent { Name =


Parent {i},Age = rnd.Next( 20 50 )};

for int j = 0 ; j < 20 ; j ++)
{
parent.Children.Add( new Person
{
Name =
"Parent {i}", Age = rnd.Next(20, 50) }; for (int j = 0; j < 20; j++) { parent.Children.Add(new Person { Name =


这篇关于如何在条件下更改数据网格的行颜色?这对我来说很重要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆