Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6572779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:02:59+00:00 2026-05-25T15:02:59+00:00

I am currently working on a C# WPF datagrid. I have a DataGrid which

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:02:59+00:00Added an answer on May 25, 2026 at 3:02 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a DataGrid in my WPF window which is bound to a database.
I am working with the the WPF Toolkit DataGrid and currently have a problem
I am currently working on a C# WPF project. I have a list which
I am currently working on a c# wpf project. I have added code to
I'm currently working out the layout of a WPF Application and seem to have
In my application i have a WPF Window that has a DataGrid in it.
I am currently working on a WPF/.Net project, In this project the user has
I'm currently working on a WPF UI and I have a combobox on my
I'm currently working on a WPF (with C# behind the scenes) system which requires
I am currently working on a WPF application where I would like to have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.