循环通过动态datagridview - .rowcount(+)在循环通过它时更改 [英] Looping through a dynamic datagridview - .rowcount (+) changing while looping through it

查看:110
本文介绍了循环通过动态datagridview - .rowcount(+)在循环通过它时更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(使用VB.Net的Windows窗体应用程序)



我有 DataGridView ,行显示距离和两个位置之间的估计时间称为路径支路数据。我已经实现了一个 CheckBox ,如果选择,它应该遍历Route Leg数据并且 Insert 当前路线腿数据下方的腿步数据。



如果CheckBox 未选中,循环将删除所有Leg Step数据 - 这部分工作正常。



问题是每次添加Leg Step时DataGridView的RowCount都会改变这会导致大多数较低的Route Leg细节线被跳过。



我试过For Each,For ... Next和While循环尝试循环动态变化的DataGridView。就好像循环的开始在循环开始时获取行数(RowCount)的快照,并且它在循环执行开始时保持原始行数。



这是我正在使用的代码。它是我希望使用的实际代码的简化:



(Windows Forms application using VB.Net)

I have a DataGridView with rows displaying the Distance and Estimated Time between two locations called the Route Leg data. I have implemented a CheckBox where if Selected it should loop through the Route Leg data and Insert the Leg Step data below the current Route Leg data.

If the CheckBox is Not Selected, a loop will remove all the Leg Step data - this part works well.

The problem is that the DataGridView's RowCount changes every time a Leg Step is added and this causes most of the lower Route Leg detail lines to be skipped.

I have tried For Each, For...Next and While loops to try and loop through the dynamically changing DataGridView. It is as if the start of the loop takes a snapshot of the number of rows (RowCount) at the start of the loop and it keeps to the original number of rows at the start of loop execution.

Here is the code I am using. It is a strip down of the actual code I wish to use:

Private Sub chkShowRouteStep_CheckedChanged(sender As Object, e As EventArgs) Handles chkShowRouteStep.CheckedChanged
        Try
            If chkShowRouteStep.Checked Then
                'Show Leg Steps
                For i As Integer = 0 To dgvQuote.RowCount - 1
                    txtRowCount.Text = i
                    If dgvQuote.Rows(i).Cells(0).Value.ToString <> "" Then
                        For j As Integer = 1 To 5
                            dgvQuote.Rows.Insert(i + j, "Step")
                        Next
                    End If
                    i += 1
                Next


            Else
                'Hide Leg Steps
                For i As Integer = dgvQuote.RowCount - 1 To 0 Step -1
                    If dgvQuote.Rows(i).Cells(0).Value.ToString = "Step" Then
                        dgvQuote.Rows.RemoveAt(i)
                    End If
                Next
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub





有没有办法可以遍历所有行,甚至那些在循环开始后添加的东西?



我尝试了什么:



For循环

对于每个循环

while循环



Is there a way where I can loop through all the lines, even those added after the loop was started?

What I have tried:

For loop
For each loop
While loop

推荐答案

我能够解决这个问题。



如何在DataGridView中动态创建显示/隐藏详细信息 [ ^ ]
I was able to solve this problem.

How to create a Show / Hide details dynamically in a DataGridView[^]


这篇关于循环通过动态datagridview - .rowcount(+)在循环通过它时更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆