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,
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)
dgvCell.Style.ForeColor = Color.Gray
dgvCell.Style.Font = New Font("Microsoft San Serif", 8, FontStyle.Strikeout)