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 325289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:12:11+00:00 2026-05-12T09:12:11+00:00

I have a list of objects (of type ‘TrackedSet’) that is data bound to

  • 0

I have a list of objects (of type ‘TrackedSet’) that is data bound to a DataGridView. When a new row is added, I want to be able to change the row appearance based on a property of the data bound object.

I can paint a custom background of the row in the RowPrePaint event, but I’m struggling to change the forecolor for the row.

From the code below, I am changing this by constantly changing the DefaultCellStyle property for the row to a predefined cell style which has the forecolour set to what I want.

Is this right? It seems clunky and just plain wrong.

Any advice appreciated.

/// <summary>
        /// Handles the drawing of each DataGridView row depending on TrackedSet error or flagged state.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void setLogDataGridView_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            if ((e.State & DataGridViewElementStates.Displayed) == DataGridViewElementStates.Displayed)
            {
                // Always disable normal painting of selection background and focus.
                e.PaintParts &= ~(DataGridViewPaintParts.SelectionBackground |
                                  DataGridViewPaintParts.Focus);

                // Get the tracked set associated with the row being painted.
                TrackedSet set = this._setLogBindingSource[e.RowIndex] as TrackedSet;

                if (set.IsFlagged || set.IsError) // Row requires custom painting.
                {
                    Color backColour1 = Color.Empty;
                    Color backColour2 = Color.Empty;

                    // Get rectangle of area to paint with custom brush.
                    Rectangle rowBounds = new Rectangle(
                        e.RowBounds.Left + 1,                                   // Location x
                        e.RowBounds.Top,                                        // Location y
                        this.setLogDataGridView.Columns.GetColumnsWidth(
                            DataGridViewElementStates.Visible) -
                        this.setLogDataGridView.HorizontalScrollingOffset + 1,  // Width
                        e.RowBounds.Height);                                    // Height

                    // Disable painting of backgrounds when custom painting row.
                    e.PaintParts &= ~(DataGridViewPaintParts.Background |
                                      DataGridViewPaintParts.ContentBackground);

                    if (set.IsFlagged) // Highlight colour.
                    {
                        backColour1 = this._highlightBackColour1;
                        backColour2 = this._highlightBackColour2;
                        this.setLogDataGridView.Rows[e.RowIndex].DefaultCellStyle = this._highlightStyle;
                    }
                    else // Error colour.
                    {
                        backColour1 = this._errorBackColour1;
                        backColour2 = this._errorBackColour2;
                        this.setLogDataGridView.Rows[e.RowIndex].DefaultCellStyle = this._errorStyle;
                    }

                    // Paint the custom background.
                    using (Brush lgb = new LinearGradientBrush(rowBounds, backColour1, backColour2, LinearGradientMode.Vertical))
                    {
                        e.Graphics.FillRectangle(lgb, rowBounds);
                    }
                }
            }
        }
  • 1 1 Answer
  • 2 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-12T09:12:11+00:00Added an answer on May 12, 2026 at 9:12 am

    You can try setting the background and foreground color in the CellFormatting event.

    void grid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        TrackedSet set = this._setLogBindingSource[e.RowIndex] as TrackedSet;
        if (set.IsFlagged)
        {
            e.CellStyle.BackColor = Color.Blue;
            e.CellStyle.ForeColor = Color.White;
        }
        else if (set.IsError)
        {
            e.CellStyle.BackColor = Color.Red;
            e.CellStyle.ForeColor = Color.Blue;
        }
    }
    

    To have your custom gradiant background you would probably still need to do that in the RowPrePaint event, but try setting the ForeColor in the CellFormatting event.

    You could also try setting the DefaultCellStyle on the row right after you bind it to the DataGridView, but I believe that certain events will trigger the DefaultCellStyle to be reset back to the RowTemplate. You may be able to set the DefaultCellStyle on the RowTemplate after you bind the data to the grid so your cell style won’t be reset.

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

Sidebar

Related Questions

I have a list that holds objects of type dynamic. When I use the
Let's say I have a list of objects of the same type. I want
I have a DataGridView object to which I've bound a list of objects (of
I have a class called Foo that defines a list of objects of type
I have IQueryable list of objects of type T which I want to transform
I have a list of type List<Iterable<Object>> and in some case the objects that
I have a list of objects of type A. In a first iteration I
I have a list of objects, can be of any type T . How
I have 2 List objects: List<int> lst1 = new List<int>(); List<int> lst2 = new
I have a list of objects and I want to remove all the ones

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.