Data Grid Views

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sc5502
    New Member
    • Jun 2014
    • 102

    #1

    Data Grid Views

    I have a program with two forms, form1 ans form2. Form1 displays a data grid view (EX1) from a SQL table. Form2 allows you to modify the search criteria in the data grid. This is accomplished by pressing a button on form2 but showing the results on form1(EX2).

    This is the code in form1:
    Code:
    Public Shared Sub testsub()
        MsgBox("Hello")
        Call dgv()     '<<<<<<< Generates an error
     End Sub
    Call dgv() generates a error: BC30369 Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class.

    What am I doing wrong?

    EX1 (code in form1):
    Code:
     Public Sub dgv()
            ' Dim dgv_rec As New DataGridView
            Dim qry As String = "select recd_id, recd_number, recd_inventorydate from t021400_records order by recd_number desc;"
            Dim cs As String = gs_SQL_CS_RecordManagement
            Try
                Dim connectionString As String = cs
                Dim connection As New SqlConnection(connectionString)
                Dim dataadapter As New SqlDataAdapter(qry, connection)
                Dim ds As New DataSet()
                connection.Open()
                dataadapter.Fill(ds, "dgv")
                ' dataadapter.Fill(ds)
                connection.Close()
                dgv_Records.DataSource = DBNull.Value
                dgv_Records.DataSource = ds
                dgv_Records.DataMember = "dgv"
    
                With dgv_Records
                    .RowsDefaultCellStyle.BackColor = Color.Bisque
                    .AlternatingRowsDefaultCellStyle.BackColor = Color.Beige
                End With
    
            Catch EX As Exception
                MsgBox(EX.ToString & " - (SQLRoutines-ReadT010200_MyManagementTeam)")
                Exit Sub
            End Try
    
        End Sub

    EX2 (code in form2):
    Code:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Form1.testsub()
    
        End Sub
    End Class
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Ah, this seems to be a duplicate question but I will answer it since you have provided more details and code snippets I can actually refer to.

    Your dgv_Records is a DataGridView that is part of Form1 and since it isn't a shared (aka static) variable it cannot be used in your shared method.

    So modify your testsub method signature to remove the "Shared" modifier:

    Code:
    Public Sub testsub()
        MsgBox("Hello")
        dgv()    
    End Sub
    And back in your Form2, either create a new instances of Form1, or have Form1 pass a reference of itself to Form2, so that in your Button1_Click you can call a non-shared method that properly refreshes the screen.

    For example (using a Property so that Form1 can pass a reference to itself to Form2):

    Code:
    Private _parentForm1 As Form1
    Public Property ParentForm1 As Form1
      Get
        return _parentForm1
      End Get
      Set(ByVal value As Form1)
        _parentForm1 = value
      End Set
    End Property
    
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      ParentForm1.testsub()
    End Sub
    Then in back in Form1, when you create an instance of Form2 to (likely when you display it) you have to use the new property to pass a reference of itself to Form2...

    In Form1 you need to:
    Code:
      Dim form2Instance As New Form2
      form2Instance.ParentForm1 = Me
    Or, instead of using a property you could modify the constructor of Form2 so that Form1 can pass a reference to itself....

    For Example:
    Code:
    Private _parentForm1 As Form1
    Public Sub New(ByVal parentForm1 As Form1)
      '...do initialization of the page stuff first and then:
      _parentForm1 = parentForm1
    End Sub
    
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      _parentForm1.testsub()
    End Sub
    And then in back in Form1, when you create an instance of Form2, you have to use to pass a reference of itself as a constructor parameter...

    In Form1 you need to:
    Code:
      Dim form2Instance As New Form2(Me)
    Does this make sense?

    Comment

    Working...