Solved

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

Posted on 2011-03-09
9
328 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

785 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