How do I use two values of a datagrid to retrieve information in Visual Studio 2008?

Karen Wilson
Karen Wilson used Ask the Experts™
on
I have a datagrid that the user makes a selection.  This selection gives me two variables.  I want to pass along these variables to a new form  which populates a datagrid.  I can pass along one of the variables by using a tag but not the second.  Here is the code for the first form's datagrid

Private Sub TC_ParameterDataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles TC_ParameterDataGridView.CellContentClick
        'get the analyte and method
        If TC_ParameterDataGridView.Rows.Count > 0 Then
            Dim rowIndex As Integer
            rowIndex = TC_ParameterDataGridView.CurrentCell.RowIndex

            Dim row As DataGridViewRow = TC_ParameterDataGridView.Rows(rowIndex)
            Dim cell As DataGridViewCell = row.Cells(0)
            Dim analysis As DataGridViewCell = row.Cells(2)
            Dim selectedCAS As String = cell.Value
            Dim selectedMethod As String = analysis.Value

            'Open new form and pass along these variables
            Dim frmAnalyteResults As New frmPFAnalyteResults
            frmAnalyteResults.Tag = selectedCAS
            'something here = selectedMethod ------ HOW TO DO I PUT IN ANOTHER TAG
            frmAnalyteResults.ShowDialog()
        End If
    End Sub

This is how I have the code set up for the frmAnalyteResults:

(I tried this to pass along the variable but it's not working)
Public selectedMethod As String = frmPFAnalytes.selectedMethod

Me.txtCasNo.Text = CStr(Me.Tag)
Me.txtWellID.Text = frmStartUpMap.well
        'Me.txtAnalysis.Text = selectedMethod  (This isn't working)

'get the rows for the datagrids
        Dim selectedCASRows = From samples In work.PFSample_tables _
                              Where samples.wellname = CStr(txtWellID.Text) _
                              Join results In work.PFResults _
                              On samples.sample_number Equals results.sample_number _
                              Where results.casno = CStr(txtCasNo.Text) _
                              AndAlso results.analysis = CStr(txtAnalysis.Text) _
                              Order By results.date_anal Descending _
                              Select results
        PFResultBindingSource.DataSource = selectedCASRows.AsQueryable

Any help would be appreciated.


Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2008

Commented:
You can define public properties on the form, and then set them for the source form.

Example:


Private m_selectedCAS As String
Public Property SelectedCAS As String
  Get
     Return m_selectedCAS
  End Get
  Set (ByVal value As String)
     m_selectedCAS = value  
  End Set
End Property
 
...
 
frmAnalyteResults.SelectedCAS = selectedCAS

Open in new window

Karen WilsonProgrammer

Author

Commented:
Does this go on the source form or the form that needs the variables?  It's early, the coffee hasn't set in...  
Karen WilsonProgrammer

Author

Commented:
Ok, I have the public property SelectedCAS()  and SelectedUnits() filled with the correct value by doing this:
Public Class frmPFAnalytes
    'This form brings up the 18 analytes of concern

    Dim work As New informationDataContext
    Private m_selectedUnit As String
    Private m_selectedCAS As String
    Public Property SelectedCAS() As String
        Get
            Return m_selectedCAS
        End Get
        Set(ByVal value As String)
            m_selectedCAS = value
        End Set
    End Property

    Public Property SelectedUnit()
        Get
            Return m_selectedUnit
        End Get
        Set(ByVal value)
            m_selectedUnit = value
        End Set
    End Property

Problem:  The old way, I would pass along the tag value like this:
'display line items in datagrid
            Dim frmAnalyteResults As New frmMiscAnalyteResults
            frmAnalyteResults.Tag = selectedCAS
            frmAnalyteResults.ShowDialog()

And on the opening form I would use the tag value like this:
Me.txtCasNo.Text = CStr(Me.Tag)

I tried picking up the variables like this:

Private Sub frmPFAnalyteResults_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.txtCasNo.Text = frmPFAnalytes.SelectedCAS
        Me.txtAnalysis.Text = frmPFAnalytes.SelectedUnit
But they are empty.  






Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Most Valuable Expert 2012
Top Expert 2008

Commented:
Where should "SelectedCAS" be coming from.  

Where you did this:

    frmAnalyteResults.Tag = selectedCAS

do this:

    frmAnalyteResults.SelectedCAS = selectedCAS

Karen WilsonProgrammer

Author

Commented:
I JUST got it figured out by hard coding the data and workin' it.

Here is the code:
Imports System.Data.Linq

Public Class frmPFAnalytes

    'This form brings up the 18 analytes of concern and sets public variables
    Dim work As New informationDataContext
    Public CAS As String = CAS
    Public UOM As String = UOM
    Private Sub frmAnalytes_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'load the datagrid
        txtWellID.Text = frmStartUpMap.well

        Dim selectAnalytes = From anals In work.TC_Parameters _
                             Order By anals.casno Ascending _
                             Select anals.casno, anals.analyte, anals.analysis, anals.units, anals.rlc, anals.rlc_source

        TC_ParameterBindingSource.DataSource = selectAnalytes

    End Sub

    Private Sub TC_ParameterDataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles TC_ParameterDataGridView.CellContentClick
        'declare some variables

        Dim CAS As String
        Dim UOM As String

        'pick an analyte
        If TC_ParameterDataGridView.Rows.Count > 0 Then
            Dim rowIndex As Integer
            rowIndex = TC_ParameterDataGridView.CurrentCell.RowIndex

            Dim row As DataGridViewRow = TC_ParameterDataGridView.Rows(rowIndex)
            Dim cell As DataGridViewCell = row.Cells(0)
            Dim unit As DataGridViewCell = row.Cells(3)
            Dim selectedCAS As String = cell.Value
            Dim selectedUnit As String = unit.Value

            'Add data to the dim's in this sub
            CAS = selectedCAS.ToString
            UOM = selectedUnit.ToString

            'pass the data along
            Dim frmAnalyteResults As New frmPFAnalyteResults
            frmAnalyteResults.txtCasNo.Text = CAS
            frmAnalyteResults.txtAnalysis.Text = UOM
            frmAnalyteResults.ShowDialog()

        End If
    End Sub
End Class

It may not work tomorrow, but today it does.  Thanks for leading me down the path.  I just couldn't get the public property to pass along the value.  


Most Valuable Expert 2012
Top Expert 2008

Commented:
In the form where you have the DataGridView, and the public properties, you need to set the field values (m_selectedCAS in my example), and then it will be available to the calling form.  If you don't set the field values, then they will be blank.
Karen WilsonProgrammer

Author

Commented:
Is this right then??

If TC_ParameterDataGridView.Rows.Count > 0 Then
            Dim rowIndex As Integer
            rowIndex = TC_ParameterDataGridView.CurrentCell.RowIndex

            Dim row As DataGridViewRow = TC_ParameterDataGridView.Rows(rowIndex)
            Dim cell As DataGridViewCell = row.Cells(0)
            Dim unit As DataGridViewCell = row.Cells(3)
------------------------------------------------------------------      
     Dim m_selectedCAS As String = cell.Value
            Dim m_selectedUnit As String = unit.Value
------------------------------------------------------------------
           
            'pass the data along
            Dim frmAnalyteResults As New frmPFAnalyteResults
            frmAnalyteResults.ShowDialog()

        End If

Most Valuable Expert 2012
Top Expert 2008
Commented:
No, those are declaration statements, you need assignment statements:

m_selectedCAS = cell.Value
m_selectedUnit = unit.Value

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial