Mr_Fulano
asked on
Event Handler firing 3 times.
Hi, I'm using VB 2005, WinForms. I have a DataGridView on one of my Forms that shows my user a list of "active and canceled Users." In the RowsAdded Event of the DataGridView, I have the code shown in the code snippet below.
The purpose of the code is to check the value of the "CanceledDate" in the DataGridView and IF the value is NOT empty (meaning there is a Cancellation Date associated with that row), then set the row's ForeColor to color=Gray and the FontStyle to Strikeout. This makes for a very nice, easy to read grid, where my users can see who canceled and who did not.
OK... that worked perfectly while I had 50 test records (rows) in the datatable, but when I increased the record count to 1000 records, then I ran into big problems -- my Form would never show! And my CPU utilization rate was flat-lined at 50% (which is a lot).
I remarked out all the code in this event and added a counter to the loop, it was then that I noticed that the counter counted through the loop 3 times. - It went all the way to row 1000, then it started over again, and one last time for 3 full passes.
Why is this happening? Shouldn't the rows be added only 1 time? Also, how can I prevent this from going into an endless loop, which seems to be happening, because my Form never shows if I use 1000 records.
I would also like to add that as soon as I remove the code below, my Form shows fine -- its has no special font formatting, but it shows.
Thanks for your help,
Fulano
The purpose of the code is to check the value of the "CanceledDate" in the DataGridView and IF the value is NOT empty (meaning there is a Cancellation Date associated with that row), then set the row's ForeColor to color=Gray and the FontStyle to Strikeout. This makes for a very nice, easy to read grid, where my users can see who canceled and who did not.
OK... that worked perfectly while I had 50 test records (rows) in the datatable, but when I increased the record count to 1000 records, then I ran into big problems -- my Form would never show! And my CPU utilization rate was flat-lined at 50% (which is a lot).
I remarked out all the code in this event and added a counter to the loop, it was then that I noticed that the counter counted through the loop 3 times. - It went all the way to row 1000, then it started over again, and one last time for 3 full passes.
Why is this happening? Shouldn't the rows be added only 1 time? Also, how can I prevent this from going into an endless loop, which seems to be happening, because my Form never shows if I use 1000 records.
I would also like to add that as soon as I remove the code below, my Form shows fine -- its has no special font formatting, but it shows.
Thanks for your help,
Fulano
Private Sub dgvUserList_RowsAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles dgvUserList.RowsAdded
For Each dgvRow As DataGridViewRow In dgvUserList.Rows
For Each dgvCell As DataGridViewCell In dgvRow.Cells
If dgvRow.Cells("CanceledDate").Value.ToString Is String.Empty Then
dgvCell.Style.Font = New Font("Microsoft San Serif", 8, FontStyle.Regular)
Else
dgvCell.Style.ForeColor = Color.Gray
dgvCell.Style.Font = New Font("Microsoft San Serif", 8, FontStyle.Strikeout)
End If
Next
Next
End Sub
ASKER
Hi Jinal, I tried your code and it didn't work. In fact, it had the opposite effect. I have 1000 rows in my DataTable and it counted rows all the way up to and over 10,000 before I manually stopped Debugging.
I did find out however that the DataGridView.Sort, which I run in the Form's LOAD Event Function was making the DataGridView reload all its rows. So, I got the loop to decrease from 3 passes down to 2. I still don't know why the rows are being added twice before the Form shows. It should only be once.
I also got the Event to fire only when I have a Date in the CanceledDate field by changing the code to this:
If Not (dgvRow.Cells("CanceledDat e").Value. ToString Is String.Empty) Then...
However, I have not solved the problem.
Thanks,
Fulano
I did find out however that the DataGridView.Sort, which I run in the Form's LOAD Event Function was making the DataGridView reload all its rows. So, I got the loop to decrease from 3 passes down to 2. I still don't know why the rows are being added twice before the Form shows. It should only be once.
I also got the Event to fire only when I have a Date in the CanceledDate field by changing the code to this:
If Not (dgvRow.Cells("CanceledDat
However, I have not solved the problem.
Thanks,
Fulano
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi CodeCruiser,
You are indeed a wealth of knowledge...!!! Your links helped A LOT !!! See below for the solution.
BTW, it is possible to use the _RowsAdded event, but it really messes things up with the Sorting. Your approach IS much better.
Thank you very much...this has taken me all day to resolve.
Fulano
You are indeed a wealth of knowledge...!!! Your links helped A LOT !!! See below for the solution.
BTW, it is possible to use the _RowsAdded event, but it really messes things up with the Sorting. Your approach IS much better.
Thank you very much...this has taken me all day to resolve.
Fulano
Private Sub dgvUserList_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgvUserList.CellFormatting
If dgvUserList.Rows(e.RowIndex).Cells("CanceledDate").Value.ToString IsNot String.Empty Then
e.CellStyle.ForeColor = Color.Gray
e.CellStyle.Font = New Font("Microsoft San Serif", 8, FontStyle.Strikeout)
End If
End Sub
ASKER
GREAT HELP!!!
Hi Fulano,
I am glad that your problem is solved.
I am glad that your problem is solved.
Open in new window