By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,116 Members | 1,509 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,116 IT Pros & Developers. It's quick & easy.

Data Grid Views

100+
P: 102
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:
Expand|Select|Wrap|Line Numbers
  1. Public Shared Sub testsub()
  2.     MsgBox("Hello")
  3.     Call dgv()     '<<<<<<< Generates an error
  4.  End Sub
  5.  
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):
Expand|Select|Wrap|Line Numbers
  1.  Public Sub dgv()
  2.         ' Dim dgv_rec As New DataGridView
  3.         Dim qry As String = "select recd_id, recd_number, recd_inventorydate from t021400_records order by recd_number desc;"
  4.         Dim cs As String = gs_SQL_CS_RecordManagement
  5.         Try
  6.             Dim connectionString As String = cs
  7.             Dim connection As New SqlConnection(connectionString)
  8.             Dim dataadapter As New SqlDataAdapter(qry, connection)
  9.             Dim ds As New DataSet()
  10.             connection.Open()
  11.             dataadapter.Fill(ds, "dgv")
  12.             ' dataadapter.Fill(ds)
  13.             connection.Close()
  14.             dgv_Records.DataSource = DBNull.Value
  15.             dgv_Records.DataSource = ds
  16.             dgv_Records.DataMember = "dgv"
  17.  
  18.             With dgv_Records
  19.                 .RowsDefaultCellStyle.BackColor = Color.Bisque
  20.                 .AlternatingRowsDefaultCellStyle.BackColor = Color.Beige
  21.             End With
  22.  
  23.         Catch EX As Exception
  24.             MsgBox(EX.ToString & " - (SQLRoutines-ReadT010200_MyManagementTeam)")
  25.             Exit Sub
  26.         End Try
  27.  
  28.     End Sub
  29.  

EX2 (code in form2):
Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  3.         Form1.testsub()
  4.  
  5.     End Sub
  6. End Class
  7.  
Feb 27 '18 #1
Share this Question
Share on Google+
1 Reply


Frinavale
Expert Mod 5K+
P: 9,731
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:

Expand|Select|Wrap|Line Numbers
  1. Public Sub testsub()
  2.     MsgBox("Hello")
  3.     dgv()    
  4. 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):

Expand|Select|Wrap|Line Numbers
  1. Private _parentForm1 As Form1
  2. Public Property ParentForm1 As Form1
  3.   Get
  4.     return _parentForm1
  5.   End Get
  6.   Set(ByVal value As Form1)
  7.     _parentForm1 = value
  8.   End Set
  9. End Property
  10.  
  11. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  12.   ParentForm1.testsub()
  13. End Sub
  14.  
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:
Expand|Select|Wrap|Line Numbers
  1.   Dim form2Instance As New Form2
  2.   form2Instance.ParentForm1 = Me
  3.  
Or, instead of using a property you could modify the constructor of Form2 so that Form1 can pass a reference to itself....

For Example:
Expand|Select|Wrap|Line Numbers
  1. Private _parentForm1 As Form1
  2. Public Sub New(ByVal parentForm1 As Form1)
  3.   '...do initialization of the page stuff first and then:
  4.   _parentForm1 = parentForm1
  5. End Sub
  6.  
  7. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  8.   _parentForm1.testsub()
  9. End Sub
  10.  
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:
Expand|Select|Wrap|Line Numbers
  1.   Dim form2Instance As New Form2(Me)
  2.  
Does this make sense?
Feb 28 '18 #2

Post your reply

Sign in to post your reply or Sign up for a free account.