Loop is taking around 10 minutes in vb

时间:2017-10-12 09:44:50

标签: windows vb.net winforms loops

 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 ?

1 个答案:

答案 0 :(得分:2)

两件事。每次更改值时,都可能导致DataGridView更新,因此在循环之前添加

DgvItemList.SuspendLayout

并在循环之后添加

    DgvItemList.ResumeLayout

您也可以将循环更改为Parallel.For循环,因此您的最终代码将类似于

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

首先尝试使用Suspend and Resume布局。您可能无法从并行化中获得大量改进。值得一试。