[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 469
  • Last Modified:

CellPainting in an infinite loop?

How to do you exit the CellPainting Event of a DataGridView correctly. After all my checks, it still loops. Below is my code:

    Private Sub cDGV2_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles cDGV2.CellPainting
        Try
            If blnFormatLoadGrid Then
                EH.ErrorMessage = ""

                If e.RowIndex >= 0 Then
                    Select Case e.ColumnIndex
                        Case 4
                            If Not IsDBNull(e.Value) Then
                                If CDec(e.Value) >= maxNonLinearity Then
                                    maxNonLinearity = e.Value
                                    iLoadRowIdx = e.RowIndex
                                End If

                                If Not (stcDefaultValues.nonLinearityMIN Is Nothing) And Not (stcDefaultValues.nonLinearityMAX Is Nothing) Then
                                    If (CDec(e.Value) < CDec(stcDefaultValues.nonLinearityMIN)) Or (CDec(e.Value) > CDec(stcDefaultValues.nonLinearityMAX)) Then
                                        cDGV2.Rows(e.RowIndex).Cells(e.ColumnIndex).Style.BackColor = Color.Salmon
                                    End If
                                End If
                            End If
                        Case 5
                            If Not IsDBNull(e.Value) Then
                                If CDec(e.Value) > CDec(maxHysteresis) Then
                                    maxHysteresis = CDec(e.Value)
                                    iHysteresisRowIdx = e.RowIndex
                                End If
                            End If

                            If Not (stcDefaultValues.hysteresisMIN Is Nothing) And Not (stcDefaultValues.hysteresisMAX Is Nothing) Then
                                If Not IsDBNull(cDGV2.Rows(e.RowIndex).Cells(5).Value) Then
                                    If CDec(cDGV2.Rows(e.RowIndex).Cells(5).Value) < CDec(stcDefaultValues.hysteresisMIN) Or CDec(cDGV2.Rows(e.RowIndex).Cells(5).Value) > CDec(stcDefaultValues.hysteresisMAX) Then
                                        cDGV2.Rows(e.RowIndex).Cells(e.ColumnIndex).Style.BackColor = Color.Salmon
                                    Else
                                        cDGV2.Rows(e.RowIndex).Cells(4).Style.BackColor = Color.PaleGreen
                                    End If
                                End If
                            End If
                    End Select
                End If

                If e.ColumnIndex = 4 And e.RowIndex = (iLoadRowCnt - 1) Then
                    If Not (stcDefaultValues.nonLinearityMIN Is Nothing) And Not (stcDefaultValues.nonLinearityMAX Is Nothing) Then
                        If Not IsDBNull(cDGV2.Rows(iLoadRowIdx).Cells(4).Value) Then
                            If CDec(cDGV2.Rows(iLoadRowIdx).Cells(4).Value) < CDec(stcDefaultValues.nonLinearityMIN) Or CDec(cDGV2.Rows(iLoadRowIdx).Cells(4).Value) > CDec(stcDefaultValues.nonLinearityMAX) Then
                                cDGV2.Rows(iLoadRowIdx).Cells(e.ColumnIndex).Style.BackColor = Color.Salmon    'If I set a breakpoint here...it never ends
                            Else
                                cDGV2.Rows(iLoadRowIdx).Cells(4).Style.BackColor = Color.PaleGreen
                            End If
                        End If
                    End If

                    If e.ColumnIndex = 5 And e.RowIndex = (iLoadRowCnt - 1) Then
                        If Not (stcDefaultValues.hysteresisMIN Is Nothing) And Not (stcDefaultValues.hysteresisMAX Is Nothing) Then
                            If Not IsDBNull(cDGV2.Rows(iHysteresisRowIdx).Cells(5).Value) Then
                                If CDec(cDGV2.Rows(iHysteresisRowIdx).Cells(5).Value) < CDec(stcDefaultValues.hysteresisMIN) Or CDec(cDGV2.Rows(iHysteresisRowIdx).Cells(5).Value) > CDec(stcDefaultValues.hysteresisMAX) Then
                                    cDGV2.Rows(iHysteresisRowIdx).Cells(e.ColumnIndex).Style.BackColor = Color.Salmon
                                Else
                                    cDGV2.Rows(iHysteresisRowIdx).Cells(4).Style.BackColor = Color.PaleGreen
                                End If
                            End If
                        End If
                    End If
                End If
            End If

        Catch ex As Exception
            EH.ErrorMessage = "frmCalibration_3/cDGV2_CellPainting() - " & ex.Message & "...Contact Engineering!" & "~E"
        End Try

        EH.ProcessMessages(Me, sbr, EH.ErrorMessage)
    End Sub

Open in new window

0
BlakeMcKenna
Asked:
BlakeMcKenna
  • 12
  • 10
1 Solution
 
Fernando SotoCommented:
Hi BlakeMcKenna;

Try adding the statement e.Handled = True as shown below to see if that corrects the issue.

    '' Add the statement here
    e.Handled = True
Catch ex As Exception
    EH.ErrorMessage = "frmCalibration_3/cDGV2_CellPainting() - " & ex.Message & "...Contact Engineering!" & "~E"
End Try

EH.ProcessMessages(Me, sbr, EH.ErrorMessage)

Open in new window

0
 
BlakeMcKennaAuthor Commented:
Hi Fernando....I actually tried that and it didn't work. I tried placing it where you suggested and still no change...
0
 
Fernando SotoCommented:
I am not understanding, I do not see any loops in the event handler so the only way that could be happening is that the event is being repeatedly called.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
BlakeMcKennaAuthor Commented:
I'm not explicitly calling the Event.

My understanding of how this event works is that the event executes for each cell in the grid. I've always thought that once the very last cell has been checked, then the Event is finished
0
 
Fernando SotoCommented:
Yes you are correct that, "My understanding of how this event works is that the event executes for each cell in the grid.", so if the grid has 10 rows and 5 columns it will be called 50 times. Another thing that will cause this event to be called is that if you place breakpoints in this event and the grid gets obscured when that part of the grid is on top again then it will re-paint the grid. The best way to debug this event and others is to place debug messages and write them out to a file this way the events do not get triggered again.
0
 
BlakeMcKennaAuthor Commented:
I'm thinking also is to just right a routine that will execute after the grid loads and have that routine make any cell color adjustments...
0
 
Fernando SotoCommented:
Is the code in the event handler doing what you want?
0
 
BlakeMcKennaAuthor Commented:
Yes...
0
 
Fernando SotoCommented:
I am guessing that you believe that this event is being called too many times. I would place a class variable for test purposes only and each time the event is called write the value out to a file and increment the value by one just to see how many times the event is called, you should have one call per cell.
0
 
Fernando SotoCommented:
I would also place and if statement just before the try keyword exit the event if it is not one of the two columns you  are looking to paint like the following.

if( e.ColumnIndex != 4 and e.ColumnIndex != 5 ) return;

Open in new window

0
 
BlakeMcKennaAuthor Commented:
Fernando,

Not sure what's going with this. I did what you suggested and when I loaded the grid with data from the DB, that event didn't even fire. I assigned the dataset to the dgv and thought that it would automatically fire the event but it didn't. Anyway, I created a function that would loop thru each row and only work with the specific columns that I need. That seems to be working really well.

I can leave this post open if you find something, please feel free to let me know. I'm always willing to try something new.

Thanks!
0
 
Fernando SotoCommented:
So you are saying that the event is no longer being called.

Can you make sure that the event handler is still being registered as an event. Go to the Property window and click on the lightning bolt icon and find the event CellPainting and make sure that the event handler name appears next to it. If it does not, click on the down arrow on the combo box and select the event from the list.
0
 
BlakeMcKennaAuthor Commented:
I placed a counter variable at the very beginning of the Event. In the routine that actually loads the grid via a database call, I placed a msgbox statement to view the count once the dgv was loaded. The statement was placed at the end of the routine. The Event does appear in the properties window so it's definitely a registered event.

In the routine that loads the dgv, I simply assign a DataTable to the dgv's DataSource property. I'm wondering if by doing it that way rather than loading each cell via a Loop prevents the CellPainting Event from firing?
0
 
Fernando SotoCommented:
No the event if registered will be call each time the a row is added to the dvv.

Don't know what else to have you try.
0
 
BlakeMcKennaAuthor Commented:
The Event definitely shows up under the Control so I know it's registered. Very strange. Anyway, as I mentioned, I'm using a routine that loops thru the rows and targets the specific Columns and it's working as it should.

Thanks for your help Fernando!
0
 
Fernando SotoCommented:
Not a problem BlakeMcKenna only wish it could have led to a more definitive solution
0
 
BlakeMcKennaAuthor Commented:
Fernando,

Not sure if I should post a new thread but the routine (called "FormatLoadGrid") that I'm calling in lieu of the CellPainting Event is working, however, when I initially click on the tabPage that contains the DGV, the color changing doesn't take place. But, when I click on another tabPage from the same Tab Control and then click back to the object tabPage...the colors show up!

This FormatLoadGrid routine is executed from the "Selected" Event of the TabControl.
0
 
Fernando SotoCommented:
You may want to try calling FormatLoadGrid from the DataGridView paint event seeming that the event is called every time the DataGridView needs to be painted.
1
 
BlakeMcKennaAuthor Commented:
I'll give it a shot Fernando...
0
 
BlakeMcKennaAuthor Commented:
Thanks Fernando...that was the issue...
0
 
Fernando SotoCommented:
Not a problem BlakeMcKenna, glad to help.
0
 
Binyamin Bair-MoshehCommented:
I'm having the same problem.
The cellpainting event is being called on a single cell, once that cell has been clicked.
It loops infinitely for that single cell.
e.handled = true is present

and I don't understand the solution given or how to execute it.
Fernando Soto

You may want to try calling FormatLoadGrid from the DataGridView paint event seeming that the event is called every time the DataGridView needs to be painted.
0
 
Fernando SotoCommented:
Hi Binyamin;

That is a function he wrote and is calling. He did not post that code here.

If you are having a similar issue please open a new question and explain your exact issues and if the code is throwing an exception the exact wording of that exception.

Thanks
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 12
  • 10
Tackle projects and never again get stuck behind a technical roadblock.
Join Now