Solved

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

Posted on 2011-03-09
9
329 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
  • 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

808 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