0

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:

      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

In this example, DGV1 would change between multiple DGV names and depending on how I do the code variable 1 could also change. The question is how to change DGV to the different names I need it to become. Otherwise I am going to have to copy and paste this code 8 times.

Claudius
  • 1,883
  • 2
  • 21
  • 34
Cheddar
  • 530
  • 4
  • 30
  • cant you pass the DGV to use? I should think using a DataSource and perhaps a DataView would be easier though. – Ňɏssa Pøngjǣrdenlarp Feb 08 '16 at 20:10
  • Going to a datasource would change all my other code though wouldn't it? Right now I am trying this "For each variable as object in me.controls" – Cheddar Feb 08 '16 at 20:11
  • if typeof variable is datagridview then – Cheddar Feb 08 '16 at 20:11
  • 1
    `private sub EggsAndYokesProc(theDGVtoUse As DataGridView)` put that code in the method, replace `DGV1` with `theDGVtoUse`. Yes, a DS might invalidate other code, but the same table and SQL is being used. much *less* code would be needed since you would not have to populate the DGVs either. – Ňɏssa Pøngjǣrdenlarp Feb 08 '16 at 20:14
  • ok give me a minuet. I will give it a shot. – Cheddar Feb 08 '16 at 20:17
  • 1
    Additionally (or alternatively), put the DGV names in an array and loop thru that. but again, if they are that similar, you could do whatever to one DataSource *once* and be done with it. Perhaps creating a DataView for each – Ňɏssa Pøngjǣrdenlarp Feb 08 '16 at 20:18
  • I like the idea of the use of an array. However, I have no experience with them. Im going to hit up the all might Oracle, Google, and try and figure that out. – Cheddar Feb 08 '16 at 20:19
  • 1
    `Private GoofyDGVs As DataGridView()` // `GoofyDGVs = New DataGridView() {dgv1, eggs, yolk, dogs, cats, chickenz, ziggy}` loop on that – Ňɏssa Pøngjǣrdenlarp Feb 08 '16 at 20:21
  • 1
    This answer is sort of a [Five Minute Starter Guide to DataSources](http://stackoverflow.com/a/33702351/1070452) and basically smart DB Objects – Ňɏssa Pøngjǣrdenlarp Feb 08 '16 at 20:32

1 Answers1

0

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........
Next
Claudius
  • 1,883
  • 2
  • 21
  • 34
  • 1
    its even easier if you store the DGV references rather than just the names – Ňɏssa Pøngjǣrdenlarp Feb 08 '16 at 20:48
  • @Plutonix, I agree with you 100%. I am going to take the time to go back and learn how toreference the DGV correctly however I need to get this into production ASAP so ill put a band aid on it for now. – Cheddar Feb 08 '16 at 20:49
  • If you are going to spend more time on it, I advise you to learn how split code among different methods. Readability and editability will improve, plus if you get into habit of doing it you will have so much less code. – Claudius Feb 08 '16 at 20:51
  • Yea, I've used the me.controls bit before. But ive only used it in an integer based application like CB1, CB2, etc. etc. Then I run an integer so its something like Dim check as combobox = me.controls("Name" & integer), combobox..,...etc. etc. – Cheddar Feb 08 '16 at 20:56