Link to home
Start Free TrialLog in
Avatar of CarlCCTVSI
CarlCCTVSIFlag for Afghanistan

asked on

set a variable from a cell in a datagridview created from the results of another datagrid view

Hi,

I've got a datagridview that I then do a "for each row" on and from that I produce another datagridview that takes its data from another table based on the results of the first dgv.

I'll attach the code to show what I mean.

What I need is to be able to set a variable based on the results of the second dgv not the first.  How on earth do I do that?  When I use row.cell(1).value I get the result from the first datagrid view not the second.

Private Sub Populate2()
        Dim strStart As String
        Dim strEnd As String

        For Each row As DataGridViewRow In dgvStatus.Rows

            If Not row.IsNewRow Then
                strStart = row.Cells(3).Value.ToString
                strEnd = row.Cells(4).Value.ToString
                DVR = row.Cells(22).Value.ToString
                Dim DifInSecs As Long = DateDiff(DateInterval.Second, CDate(strStart), CDate(strEnd))
                row.Cells(25).Value = DifInSecs
                Cameras = ""
                SQL2 = "SELECT Name, FTPFolder FROM dvrinfo WHERE Name='" & DVR & "'"
                ConfEmail = row.Cells(24).Value
                DurInSecs = DifInSecs
                Subject = row.Cells(1).Value
                IncDate = row.Cells(2).Value
                StartTime = strStart


                Try
                    conn.Open()
                    Try
                        myCommand2.Connection = conn
                        myCommand2.CommandText = SQL2
                        myAdapter2.SelectCommand = myCommand2
                        myAdapter2.Fill(myData2)
                        DVRDetails.DataSource = myData2
                        DVRDetails.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
                        IniLoc = row.Cells(1).Value 'I need this variable populated from the DVRDetails datagrid view.
                    Catch myerror As MySqlException
                        MsgBox("There was an error reading from the database: " & myerror.Message)
                    End Try

                Catch myerror As MySqlException
                    MessageBox.Show("Error connecting to the database: " & myerror.Message)
                Finally
                    If conn.State <> ConnectionState.Closed Then conn.Close()
                End Try

                If row.Cells(5).Value.ToString = "1" Then
                    Cameras = "All"
                Else
                    If row.Cells(6).Value.ToString = "1" Then
                        Cameras = Cameras & "1"
                    End If
                    If row.Cells(7).Value.ToString = "1" Then
                        If Cameras = "" Then
                            Cameras = Cameras & "2"
                        Else
                            Cameras = Cameras & ",2"
                        End If
                    End If
                    If row.Cells(8).Value.ToString = "1" Then
                        If Cameras = "" Then
                            Cameras = Cameras & "3"
                        Else
                            Cameras = Cameras & ",3"
                        End If
                    End If
                    If row.Cells(9).Value.ToString = "1" Then
                        If Cameras = "" Then
                            Cameras = Cameras & "4"
                        Else
                            Cameras = Cameras & ",4"
                        End If
                    End If
                    If row.Cells(10).Value.ToString = "1" Then
                        If Cameras = "" Then
                            Cameras = Cameras & "5"
                        Else
                            Cameras = Cameras & ",5"
                        End If
                    End If
                    If row.Cells(11).Value.ToString = "1" Then
                        If Cameras = "" Then
                            Cameras = Cameras & "6"
                        Else
                            Cameras = Cameras & ",6"
                        End If
                    End If
                    If row.Cells(12).Value.ToString = "1" Then
                        If Cameras = "" Then
                            Cameras = Cameras & "7"
                        Else
                            Cameras = Cameras & ",7"
                        End If
                    End If
                    If row.Cells(13).Value.ToString = "1" Then
                        If Cameras = "" Then
                            Cameras = Cameras & "8"
                        Else
                            Cameras = Cameras & ",8"
                        End If
                    End If
                    If row.Cells(14).Value.ToString = "1" Then
                        If Cameras = "" Then
                            Cameras = Cameras & "9"
                        Else
                            Cameras = Cameras & ",9"
                        End If
                    End If
                    If row.Cells(15).Value.ToString = "1" Then
                        If Cameras = "" Then
                            Cameras = Cameras & "10"
                        Else
                            Cameras = Cameras & ",10"
                        End If
                    End If
                    If row.Cells(16).Value.ToString = "1" Then
                        If Cameras = "" Then
                            Cameras = Cameras & "11"
                        Else
                            Cameras = Cameras & ",11"
                        End If
                    End If
                    If row.Cells(17).Value.ToString = "1" Then
                        If Cameras = "" Then
                            Cameras = Cameras & "12"
                        Else
                            Cameras = Cameras & ",12"
                        End If
                    End If
                    If row.Cells(18).Value.ToString = "1" Then
                        If Cameras = "" Then
                            Cameras = Cameras & "13"
                        Else
                            Cameras = Cameras & ",13"
                        End If
                    End If
                    If row.Cells(19).Value.ToString = "1" Then
                        If Cameras = "" Then
                            Cameras = Cameras & "14"
                        Else
                            Cameras = Cameras & ",14"
                        End If
                    End If
                    If row.Cells(20).Value.ToString = "1" Then
                        If Cameras = "" Then
                            Cameras = Cameras & "15"
                        Else
                            Cameras = Cameras & ",15"
                        End If
                    End If
                    If row.Cells(21).Value.ToString = "1" Then
                        If Cameras = "" Then
                            Cameras = Cameras & "16"
                        Else
                            Cameras = Cameras & ",16"
                        End If
                    End If
                End If

                row.Cells(26).Value = Cameras
                Call SendEmail()
                Call writeini()
                'MsgBox("Difference is " & DifInSecs.ToString)
                'MessageBox.Show(row.Cells(22).Value.ToString)

            End If

        Next

        ' Call Delay(3)
        cmdStatus.Text = "Database Refreshed"
    End Sub

Open in new window

Avatar of nepaluz
nepaluz
Flag of United Kingdom of Great Britain and Northern Ireland image

the row you arereferencing from here:

For Each row As DataGridViewRow In dgvStatus.Rows

refers to the first datagrid. I think you need to reference DVRDetails, which I assume is the second datagrid
Avatar of CarlCCTVSI

ASKER

That is very true, the question is - how do I do it?

I've tried various ways and ended up with less hair than I started with.
you may have to do another iteration, not sure ...

For Each Xrow As DataGridViewRow In  DVRDetails.Rows
Actually I am sure now. After populating the second datagrid DVRDetails you start iterating it to get your values, eg

For Each Xrow As DataGridViewRow In  DVRDetails.Rows
   If Xrow.Cells(5).Value.ToString = "1" Then
       Cameras = "All"
   Else
        .......

Open in new window

In the code above there is only reference to one datagrid view (dgvStatus) and in dgvStatus you set colum (25) and (26) in dgvStatus.

If the underlying datasource of each datagridview is the same data an edit in one will result in an edit in the other.  If you want to separate the data you may want to clone it.

Regards,

Corey
I see what it is supposed to do but the problem is it still returns the row.cells(1).Value from the original datagridview.  

Whether I use the 2nd For Each xrow or not the result is the same.  I must be missing something somewhere.
ASKER CERTIFIED SOLUTION
Avatar of nepaluz
nepaluz
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Superb!!

I was still referring to row.cells(1).value rather than xrow.cells(1).value

It working a treat.  Thanks nepaluz
thats why I thought I should show it in BOLD!
Pleasure is all mine.