Solved

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

Posted on 2011-03-09
9
321 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
 
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now