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

  • Home
  • SEARCH
  • 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 6159423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:07:47+00:00 2026-05-23T21:07:47+00:00

I have a WPF Datagrid populated using a Dataset. I am trying to change

  • 0

I have a WPF Datagrid populated using a Dataset. I am trying to change the font colour of the data in two columns. I have it working using

REMOVED OLD CODE

But it isn’t holding the colours very well especially when there is scrolling on the grid. It is also slow.

Is it possible to do this using an IValueCoverter or is there some other more efficient way to achieve this?

EDIT

I have tried to take a new approach to this problem. I have created a class to return a bool and then use that bool to determine whether the font will be green or red.

CLASS

class EqualValuesColourConverter
{
    public static void ChangeColours(int pQty, int pPri, int pTot, int gQty, int gPri, int gTot)
    {
        int iqty = pQty;
        int gqty = gQty;
        int iprice = pPri;
        int gprice = gPri;
        int itotal = pTot;
        int gtotal = gTot;

        bool fontColor = true;

        if ((iqty == gqty) && (iprice == gprice) && (itotal == gtotal)) fontColor = true;
        else fontColor = false;

    }
 }

CALL TO CLASS

 string iqty = ((DataRowView)DgInvoiceLines.SelectedItem)["Inv_Quantity"].ToString();
            string gqty = ((DataRowView)DgInvoiceLines.SelectedItem)["Grn_Quantity"].ToString();
            string iprice = ((DataRowView)DgInvoiceLines.SelectedItem)["Inv_Price"].ToString();
            string gprice = ((DataRowView)DgInvoiceLines.SelectedItem)["Grn_Price"].ToString();
            string itotal = ((DataRowView)DgInvoiceLines.SelectedItem)["Inv_Total"].ToString();
            string gtotal = ((DataRowView)DgInvoiceLines.SelectedItem)["Grn_Total"].ToString();

            int pQty = int.Parse(iqty);
            int pPri = int.Parse(iprice);
            int pTot = int.Parse(itotal);
            int gQty = int.Parse(gqty);
            int gPri = int.Parse(gprice);
            int gTot = int.Parse(gtotal);

            EqualValuesColourConverter.ChangeColours(pQty, pPri, pTot, gQty, gPri, gTot);

XAML

<DataGridTextColumn Width="61" Header="Inv_Quantity" Binding="{Binding Inv_Quantity}">
<DataGridTextColumn.CellStyle>
    <Style TargetType="{x:Type DataGridCell}">
        <Setter Property="Foreground" Value="Green"/>
        <Style.Triggers>
            <DataTrigger Binding="{Binding EqualValuesColourConverter}" Value="False" >
                <Setter Property="Foreground" Value="Red" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</DataGridTextColumn.CellStyle>

I am trying to get it to work in two columns but it will only change the font colour in one.

Can anyone help?

  • 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-23T21:07:48+00:00Added an answer on May 23, 2026 at 9:07 pm
    int i = DgInvoiceLines.SelectedIndex;
                string iqty = ((DataRowView)DgInvoiceLines.SelectedItem)["Inv_Quantity"].ToString();
                string gqty = ((DataRowView)DgInvoiceLines.SelectedItem)["Grn_Quantity"].ToString();
                string iprice = ((DataRowView)DgInvoiceLines.SelectedItem)["Inv_Price"].ToString();
                string gprice = ((DataRowView)DgInvoiceLines.SelectedItem)["Grn_Price"].ToString();
                string itotal = ((DataRowView)DgInvoiceLines.SelectedItem)["Inv_Total"].ToString();
                string gtotal = ((DataRowView)DgInvoiceLines.SelectedItem)["Grn_Total"].ToString();
    
                DataGridCell InvQtyCell = GetCell(i, 2);
                DataGridCell GrnQtyCell = GetCell(i, 3);
                DataGridCell InvPriCell = GetCell(i, 4);
                DataGridCell GrnPriCell = GetCell(i, 5);
                DataGridCell InvTotCell = GetCell(i, 6);
                DataGridCell GrnTotCell = GetCell(i, 7);
    
                string InvoiceCellContentType = InvQtyCell.Content.GetType().Name.ToString();
                string GRNCellContentType = GrnQtyCell.Content.GetType().Name.ToString();
                string InvPriContentType = InvPriCell.Content.GetType().Name.ToString();
                string GrnPriContentType = GrnPriCell.Content.GetType().Name.ToString();
                string InvTotCellType = InvTotCell.Content.GetType().Name.ToString();
                string GrnTotCelType = GrnTotCell.Content.GetType().Name.ToString();
    
                if (iqty == gqty) 
                {
                    if (InvoiceCellContentType == "TextBlock") ((TextBlock)InvQtyCell.Content).Foreground = Brushes.DarkGreen;
                    else if (InvoiceCellContentType == "TextBox") ((TextBox)InvQtyCell.Content).Foreground = Brushes.DarkGreen;
                    if (GRNCellContentType == "TextBlock") ((TextBlock)GrnQtyCell.Content).Foreground = Brushes.DarkGreen;
                    else if (GRNCellContentType == "TextBox") ((TextBox)GrnQtyCell.Content).Foreground = Brushes.DarkGreen;
                }
                else
                {
                    if (InvoiceCellContentType == "TextBlock") ((TextBlock)InvQtyCell.Content).Foreground = Brushes.Red;
                    else if (InvoiceCellContentType == "TextBox") ((TextBox)InvQtyCell.Content).Foreground = Brushes.Red;
                    if (GRNCellContentType == "TextBlock") ((TextBlock)GrnQtyCell.Content).Foreground = Brushes.Red;
                    else if (GRNCellContentType == "TextBox") ((TextBox)GrnQtyCell.Content).Foreground = Brushes.Red;
                }
    
                if (iprice == gprice) 
                {
                    if (InvPriContentType == "TextBlock") ((TextBlock)InvPriCell.Content).Foreground = Brushes.DarkGreen;
                    else if (InvPriContentType == "TextBox") ((TextBox)InvPriCell.Content).Foreground = Brushes.DarkGreen;
                    if (GrnPriContentType == "TextBlock") ((TextBlock)GrnPriCell.Content).Foreground = Brushes.DarkGreen;
                    else if (GrnPriContentType == "TextBox") ((TextBox)GrnPriCell.Content).Foreground = Brushes.DarkGreen;
                }
                else
                {
                    if (InvPriContentType == "TextBlock") ((TextBlock)InvPriCell.Content).Foreground = Brushes.Red;
                    else if (InvPriContentType == "TextBox") ((TextBox)InvPriCell.Content).Foreground = Brushes.Red;
                    if (GrnPriContentType == "TextBlock") ((TextBlock)GrnPriCell.Content).Foreground = Brushes.Red;
                    else if (GrnPriContentType == "TextBox") ((TextBox)GrnPriCell.Content).Foreground = Brushes.Red;
                }
    

    Just in case any one wanted the answer this actually works as long as it is called in

    DgInvoiceLines_CellEditEnding and DgInvoiceLines_CurrentCellChanged only.

    I had called it in DgInvoiceLines_SelectionChanged which seems to make it behave strangely.

    HTH!

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

Sidebar

Related Questions

I have a wpf datagrid that has it's columns generated dynamically in code and
There is a WPF DataGrid with columns that have checkboxes. These checkboxes are already
I have a DataGrid, and in that grid, some of the columns are marked
I have a WPF MVVM prism application to upload files. I filled a datagrid
I am working on a recreation of a search control using the MVVM pattern.
The default behaviour of a WPF DataGrid is to select when a row is
I have been struggling with this for a while now. I have a Master
Currently I am searching for the best way for paginating the records in WPF
Have following code: public interface ITest { string St1 { get; } } public

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.