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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:50:47+00:00 2026-05-25T14:50:47+00:00

I have a datagridview control that where I need to color the rows based

  • 0

I have a datagridview control that where I need to color the rows based on a value in one of the cells in each row. I’m using the CellFormatting event like so:

Private Sub DGDisplay_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgDisplay.CellFormatting

    Dim intRowIndex As Integer = e.RowIndex 'This is zero when sorting.....
    Dim CurrentRow As DataGridViewRow = dgDisplay.Rows(intRowIndex)
    Dim strTestValue As String = CurrentRow.Cells("Status").Value

    Select Case UCase(strTestValue)
        Case "WARNING"
            CurrentRow.DefaultCellStyle.BackColor = Color.PeachPuff
        Case "ERRMESSAGE"
            CurrentRow.DefaultCellStyle.BackColor = Color.Salmon
    End Select
End Sub

This works fine when the grid loads and when I scroll it, etc. But when I click on the column headers to sort the grid, e.RowIndex is always zero and all of the rows get the formatting of the first row…

Why isn’t this working when the grid sorts?

EDIT:
Joakim was on the right track but the following code works correctly:

Private Sub dgDisplay_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles dgDisplay.CellPainting

    If e.RowIndex < 0 Then
        Exit Sub
    End If

    Dim intRowIndex As Integer = e.RowIndex
    Dim CurrentRow As DataGridViewRow = dgDisplay.Rows(intRowIndex)
    Dim strTestValue As String = CurrentRow.Cells("Status").Value

    Select Case UCase(strTestValue)
        Case "WARNING"
            CurrentRow.DefaultCellStyle.BackColor = Color.PeachPuff
        Case "ERRMESSAGE"
            CurrentRow.DefaultCellStyle.BackColor = Color.Salmon
    End Select
End Sub

For some reason, e.RowIndex is set correctly here but not on the other methods. The only thing you have to worry about here is it can be -1. But when I tried to use other methods, including PrePaint, I had to deal with it always coming up zero on a sort. If I exclude the zero case, like I excluded the negative one case about, then the first row is always white!!! Madness… I’m not sure why this works, but it does. It also produces no flicker beyond what I got using the CellFormatting event.

IF ANYONE CAN EXPLAIN THE REASON WHY e.RowIndex IS BEHAVING SO WEIRD OR OFFER A BETTER WAY OF DOING THIS THEY WILL GET THE ACCEPTED ANSWER!

  • 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-25T14:50:47+00:00Added an answer on May 25, 2026 at 2:50 pm
    Private Sub dgDisplay_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles dgDisplay.CellPainting
    
    If e.RowIndex < 0 Then
        Exit Sub
    End If
    
    Dim intRowIndex As Integer = e.RowIndex
    Dim CurrentRow As DataGridViewRow = dgDisplay.Rows(intRowIndex)
    Dim strTestValue As String = CurrentRow.Cells("Status").Value
    
    Select Case UCase(strTestValue)
        Case "WARNING"
            CurrentRow.DefaultCellStyle.BackColor = Color.PeachPuff
        Case "ERRMESSAGE"
            CurrentRow.DefaultCellStyle.BackColor = Color.Salmon
    End Select
    

    End Sub

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

Sidebar

Related Questions

I have a DataGridView in a form that has a data table as the
So basically I've got 2 DataGridView and I need to copy the rows from
I have a basic WinForms app with d DataGridView (named dgvHardware ) control on
I am new to programming in visual c++.I am using c++ codings.I have datagridview
I have a C# program that selects data from two different database files and
I have a DataGridView with a user definable number of columns (anywhere from ~6-60)
I have a DataGridView bound to a List[of Parts]. The last item in the
Let me be more precise. In my winforms project im creating classes to manage/create
This is the real challenge I brings here for you. Solve it if you

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.