Solved

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

Posted on 2011-03-09
9
332 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:CarlCCTVSI
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 17

Expert Comment

by:nepaluz
ID: 35083728
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
0
 

Author Comment

by:CarlCCTVSI
ID: 35083744
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.
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 35083851
you may have to do another iteration, not sure ...

For Each Xrow As DataGridViewRow In  DVRDetails.Rows
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 17

Expert Comment

by:nepaluz
ID: 35083995
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

0
 
LVL 13

Expert Comment

by:Corey2
ID: 35084034
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
0
 

Author Comment

by:CarlCCTVSI
ID: 35085587
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.
0
 
LVL 17

Accepted Solution

by:
nepaluz earned 500 total points
ID: 35085712
If you use the Xrow.cells(1).Value in the second iteration, unless your data from the DB is wrong, then you should get the correct result (or at least the result in the second DataGrid).

-> Try calling DVRDetails.Refresh() after binding it to the datasource.

SIDEPOINT: Does the datasource to DG2 contain multiple records? If not, why do you need to bind it to the datagridview?
0
 

Author Comment

by:CarlCCTVSI
ID: 35086177
Superb!!

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

It working a treat.  Thanks nepaluz
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 35086426
thats why I thought I should show it in BOLD!
Pleasure is all mine.
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question