-1
 For Each drow As DataGridViewRow In DgvItemList.Rows
            drow.Cells("strSrNo").Value = drow.Index + 1
        Next

I have more than 3500 records in DgvItemList. I just give to numbering to that records but it tool 9 to 10 minutes for that.

How to reduce this time ?

Bhoomi
  • 89
  • 9
  • https://stackoverflow.com/questions/5817632/beginupdate-endupdate-for-datagridview-request, maybe? Windows messages make it Windows-specific, though. – Ry- Oct 12 '17 at 09:49
  • how to do this? – Bhoomi Oct 12 '17 at 09:56
  • What are you actually trying to do? As in: *why* do you need to assign what looks like some sort of serial number to data that is already in the grid? EDIT: also, is your grid bound to a DataTable or something? – s.m. Oct 12 '17 at 10:08
  • yes data is binding by datatable. I just need to give number to the row . – Bhoomi Oct 12 '17 at 10:11
  • You state that the DGV is bound to a DataTable. Do you really need the row number stored in the DataTable or is this a _Display Only_ requirement? If it is only for display, you can use an unbound column and paint the row number in code. If you stay with your current code, determine the column index of "strSrNo" before the loop and use the value in the loop; this will prevent looking up the column index on each iteration. – TnTinMn Oct 14 '17 at 04:38

1 Answers1

2

Two things. Each time you change the value, it could cause the DataGridView to update, so just before your loop, add

DgvItemList.SuspendLayout

and after the loop, add

    DgvItemList.ResumeLayout

You could also change the loop to a Parallel.For loop, so your final code would be something like

DgvItemList.SuspendLayout 
Parallel.For(0, DgvItemList.Rows.Count, Sub(index As Integer)
                                        DgvItemList.Rows(index).Cells("strSrNo").Value = DgvItemList.Rows(index).Index + 1
                                            End Sub)
DgvItemList.ResumeLayout

Try it with just the Suspend and Resume layout first. You may not get a vast amount of improvement from the parallelization. Worth a go though.

David Wilson
  • 4,369
  • 3
  • 18
  • 31
  • Shouldn't end condition of the Parallel.For be the row count minus 1? – dwilliss Oct 12 '17 at 13:32
  • I've never used `Parallel.For`, but doesn't it require you to invoke when accessing the UI, just like with regular threads/tasks? – Visual Vincent Oct 12 '17 at 15:24
  • @dwilliss no. Just to confuse people, a regular for.next loop say from 0 to 100 will loop 101 times, but a parallel.for loop will only execute from 0 to 99, so you don't need to subtract 1 from the end value in the case of zero based indexes. On the other side of the coin of course, if the end of your loop is obtained from array.getupperbound, you might need to add 1 – David Wilson Oct 13 '17 at 01:33
  • @VisualVincent Perhaps, but I'm guessing that as the code only changes data and possibly doesn't call any methods that change the UI there is no cross thread problem. It seems to work fine. If you put in something like a call to the .Update method, it does hang with a cross threading error. – David Wilson Oct 13 '17 at 01:53
  • But `TextBox.Text` is also data, yet it throws an exception because it causes the text box to be redrawn. The value of the cell must also be redrawn when you change it, so I think you would normally have to invoke here as well (unless the DGV has a different way of detecting changes). – Visual Vincent Oct 13 '17 at 05:24
  • Timing is the same as before after using resume and suspend layout @DavidWilson – Bhoomi Oct 13 '17 at 06:54
  • @VisualVincent I know. It's odd. I've run it on a small dgv row count and it seems to work. – David Wilson Oct 13 '17 at 13:00
  • @Bhoomi Does the parallel.For improve things? – David Wilson Oct 13 '17 at 13:00