Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
563 views
in Technique[技术] by (71.8m points)

c# - DataGrid Row Background Based On Cell Value

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.

" + ex.Message + "

Error 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...