基于单元格值的DataGrid行背景

发布于 2024-12-03 19:27:28 字数 1213 浏览 0 评论 0原文

我目前正在开发 C# WPF 数据网格。我有一个 DataGrid,它具有自动生成的列,代码连接到 SQLite 数据库并创建一个数据集,然后该数据集被设置为 DataGrid ItemsSource。

下面是带有 DataGrid 的 XAML 的代码

<DataGrid AutoGenerateColumns="True"
          Margin="12,71,12,32"
          Name="tblLog"
          ColumnWidth="*"
          CanUserResizeRows="False"
          AreRowDetailsFrozen="False"
          CanUserAddRows="True"
          CanUserDeleteRows="True"
          IsReadOnly="True"
          MouseDoubleClick="tblLog_MouseDoubleClick">                
</DataGrid>

下面是为 DataGrid 设置 ItemsSource 的代码

try
{
    DataSet ds = new DataSet();
    SQLiteDataAdapter da = new SQLiteDataAdapter(query, db.conn);
    da.Fill(ds);

    //tblGrid.AutoGenerateColumns = true;
    tblGrid.ItemsSource = ds.Tables[0].DefaultView;                    
}
catch (SQLiteException ex)
{
    MessageBox.Show("Unable to retrieve logins from database.\n\n" + ex.Message + "\n\nError Code: " + ex.ErrorCode);
}

数据库中显示的列(自动生成)是 ID、日期、时间、状态。 我需要做的是,如果状态列的一行中的值等于错误,则更改该行的背景颜色。

我假设我需要在 DataGrid 标记中添加某种样式标记和 DataTriggers,但不确定我需要什么。我尝试设置 ItemsSource 的代码会显示一条错误,指出在添加 ItemsSource 之前源需要为空。

感谢您提供的任何帮助。

I am currently working on a C# WPF datagrid. I have a DataGrid which has auto generated columns and the code connects to an SQLite Database and creates a dataset and then this dataset is set as the DataGrid ItemsSource.

Below is the code with the XAML of the DataGrid

<DataGrid AutoGenerateColumns="True"
          Margin="12,71,12,32"
          Name="tblLog"
          ColumnWidth="*"
          CanUserResizeRows="False"
          AreRowDetailsFrozen="False"
          CanUserAddRows="True"
          CanUserDeleteRows="True"
          IsReadOnly="True"
          MouseDoubleClick="tblLog_MouseDoubleClick">                
</DataGrid>

And below is the code to set the ItemsSource for the DataGrid

try
{
    DataSet ds = new DataSet();
    SQLiteDataAdapter da = new SQLiteDataAdapter(query, db.conn);
    da.Fill(ds);

    //tblGrid.AutoGenerateColumns = true;
    tblGrid.ItemsSource = ds.Tables[0].DefaultView;                    
}
catch (SQLiteException ex)
{
    MessageBox.Show("Unable to retrieve logins from database.\n\n" + ex.Message + "\n\nError Code: " + ex.ErrorCode);
}

The columns that are shown in the database (auto generated) are ID, date, time, status.
What I need to be able to do is if the value in a row of the status column equals Error change the background colour of that row.

I assume I need to add some sort of styling tags and DataTriggers within the DataGrid tags but not sure what I need. Anything I have tried to the code that sets the ItemsSource displays an error saying that the Source needs to be empty before adding the ItemsSource.

Thanks for any help you can provide.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不爱素颜 2024-12-10 19:27:28

您可以使用 DataTrigger 来执行此操作。

这是一个快速示例。我创建了一个名为 Person 的类,其属性为 Name、Age 和 Active。

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public bool Active { get; set; }
}

在主窗口的构造函数中,我将 3 个 Person 对象添加到列表中,然后将该列表绑定到 DataGrid

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

        List<Person> people = new List<Person>();
        people.Add(new Person() 
        { 
            Name = "John Doe",
            Age = 32,
            Active = true
        });
        people.Add(new Person()
        {
            Name = "Jane Doe",
            Age = 30,
            Active = true
        });
        people.Add(new Person()
        {
            Name = "John Adams",
            Age = 64,
            Active = false
        });
        tblLog.ItemsSource = people;
    }
}

然后,在 MainWindow 的 XAML 中,我创建一个 DataTrigger 样式作为资源。

<Window.Resources>
    <Style TargetType="DataGridRow">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Active}" Value="False">
                <Setter Property="Background" Value="Red" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

此触发器的作用是从 DataGridRow 中的 Person 对象的 Active 字段中获取值,如果该值为 false,则它会变为背景颜色该行的颜色变为红色。

You can use a DataTrigger to do this.

Here is a quick sample. I created a class called Person with the properties Name, Age, and Active.

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public bool Active { get; set; }
}

In the constructor of the main window, I add 3 Person objects to a list, then bind that list to the DataGrid.

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

        List<Person> people = new List<Person>();
        people.Add(new Person() 
        { 
            Name = "John Doe",
            Age = 32,
            Active = true
        });
        people.Add(new Person()
        {
            Name = "Jane Doe",
            Age = 30,
            Active = true
        });
        people.Add(new Person()
        {
            Name = "John Adams",
            Age = 64,
            Active = false
        });
        tblLog.ItemsSource = people;
    }
}

Then in the XAML for the MainWindow, I create a DataTrigger style as a resource.

<Window.Resources>
    <Style TargetType="DataGridRow">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Active}" Value="False">
                <Setter Property="Background" Value="Red" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

What this trigger does is it takes the value from the Active field from the Person object that is in the DataGridRow, and if that value is false, then it turns to background color of the row to red.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文