Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过多个DGV循环并通过每个DGV运行循环,vb.net_Vb.net_Loops_Datagridview - Fatal编程技术网

通过多个DGV循环并通过每个DGV运行循环,vb.net

通过多个DGV循环并通过每个DGV运行循环,vb.net,vb.net,loops,datagridview,Vb.net,Loops,Datagridview,我目前正在使用带有SQL后端的vb.net windows窗体。我有一个windows窗体,它有多个DataGridView,我需要运行所有行项目,并对每一行运行sql检查。我当前的代码很长,需要我一次又一次地复制和粘贴代码,而这个表单已经推送了1.2k行代码,所以我想保持简短。考虑到每个DGV都有一个特定的名称,我想知道是否可以生成一个字符串列表来循环使用或以某种方式使用一个窗格。以下是我迄今为止的代码: Try Dim Chickenz As String

我目前正在使用带有
SQL
后端的vb.net windows窗体。我有一个windows窗体,它有多个DataGridView,我需要运行所有行项目,并对每一行运行sql检查。我当前的代码很长,需要我一次又一次地复制和粘贴代码,而这个表单已经推送了1.2k行代码,所以我想保持简短。考虑到每个DGV都有一个特定的名称,我想知道是否可以生成一个字符串列表来循环使用或以某种方式使用一个窗格。以下是我迄今为止的代码:

      Try
        Dim Chickenz As String
        Dim MyDogTank As String
        'Set up loop
        For cn As Integer = 1 To DGV1.RowCount
            Dim variable1 As Date = DGV1.Rows(cn - 1).Cells(1).Value
            Using conn1 As New SqlConnection(connstring)
                conn1.Open()
                Using comm1 As New SqlCommand (sqlcommand, conn1)
                    comm1.Parameters.AddWithValue("@Parameter", variable1)
                    Dim dt As New DataTable
                    Dim sql As New SqlDataAdapter(comm1)
                    sql.Fill(dt)
                    For Each row As DataRow In dt.Rows
                        Chickenz = row.Item("col2")
                        MyDogTank = row.Item("col1")
                        '--------Sql to check 
                        Using conn2 As New SqlConnection(connstring)
                            conn2.Open()
                            Using comm2 As New SqlCommand(sqlcommand, conn2)
                                comm2.Parameters.AddWithValue("@Job", Chickenz)
                                Dim Eggz As New DataTable
                                Dim Yolk As New SqlDataAdapter(comm2)
                                Yolk.Fill(Eggz)
                                If Eggz.Rows.Count >= 1 Then
                                    MsgBox("warningbox")
                                End If
                            End Using
                            conn2.Close()
                        End Using
                    Next
                End Using
                conn1.Close()
            End Using
        Next

    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try

在本例中,DGV1将在多个DGV名称之间更改,并且根据我的操作方式,代码变量1也可能更改。问题是如何将DGV更改为我需要的不同名称。否则,我将不得不复制并粘贴此代码8次。

因为您知道DataGridView的名称

Dim DGVNames as string() = {"DGV1", "DGV2", "DGV3","Names of datagridviews"}

For Each DGVN As String In DGVNames
    Dim DGV As DataGridView = CType(Me.Controls(DGVN), DataGridView)
    rest of your code goes here........
Next

你不能把DGV递给我使用吗?我认为使用数据源和数据视图可能会更容易。
private sub eggs和yokesproc(thedgvtuse As DataGridView)
将该代码放入方法中,用
thedgvtuse
替换
DGV1
。是的,DS可能会使其他代码无效,但使用的是相同的表和SQL。由于不必填充DGV,因此需要的代码要少得多。此外(或者可选地),将DGV名称放入数组中并循环通过该数组。但同样,如果它们如此相似,您可以对一个数据源执行任何操作一次,然后使用它。也许为每个
私有GoofyDGVs创建一个DataView作为DataGridView()
/
GoofyDGVs=New DataGridView(){dgv1,鸡蛋,蛋黄,狗,猫,鸡,ziggy}
循环,这个答案是一种基本上智能的DB对象,如果存储DGV引用而不仅仅是names@Plutonix,我百分之百同意你。我将花时间回去学习如何正确引用DGV,但是我需要尽快将其投入生产,因此我现在会在其上贴上创可贴。如果您打算在其上花费更多时间,我建议您学习如何在不同方法之间拆分代码。可读性和可编辑性将会提高,另外,如果你养成了这样做的习惯,你将拥有更少的代码。但我只在基于整数的应用程序中使用它,如CB1、CB2等。然后我运行一个整数,使其类似于Dim check,如combobox=me.controls(“Name”&integer)、combobox….等。等