Home Ask Login Register

Developers Planet

Your answer is one click away!

Cheddar February 2016

Loop through multiple DGVS and run loops through each one, vb.net

I am currently using vb.net windows forms with an SQL back end. I have a windows form that has multiple Datagridviews and I need to run through all the line items and run an sql check on each row. My curent code is long and would require me to copy and paste the code over and over again and this form is already pushing 1.2k lines of code so I want to keep it short. Considering each DGV has a specific name I was wondering if I could generate a list of strings to loop through or somehow use a pane. Here is my code thus far:

        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)
                Using comm1 As New SqlCommand (sqlcommand, conn1)
                    comm1.Parameters.AddWithValue("@Parameter", variable1)
                    Dim dt As New DataTable
                    Dim sql As New SqlDataAdapter(comm1)
                    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)
                            Using comm2 As New SqlCommand(sqlcommand, conn2)
                                comm2.Parameters.AddWithValue("@Job", Chickenz)
                                Dim Eggz As New DataTable
                                Dim Yolk As New SqlDataAdapter(comm2)
                                If Eggz.Rows.Count >= 1 Then
                                End If
                            End Using


Claudius February 2016

Since you know names of Datagridviews

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........

Post Status

Asked in February 2016
Viewed 3,614 times
Voted 12
Answered 1 times


Leave an answer

Quote of the day: live life