473,387 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Data Grid Views

102 64KB
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
1 1574
Frinavale
9,735 Expert Mod 8TB
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

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

Similar topics

2
by: Jordan O'Hare | last post by:
Hello Everyone, I am after some help with the following: I have a windows application that contains a list box and two data grids. All three controls are binded to a dataset that contains...
5
by: pmud | last post by:
Hi, I need to display columns in a data grid based on 7 different queries. Now I have 32 questions: 1. Is it possble to have 1 single data adapter with 7 queries & 1 data set or do I need to...
3
by: Snake | last post by:
I have a vb .net program which fills a data grid upon form load from an acccess database. This works great. Now, I have to add a combo box and use it to alter the underlying sql statement and...
2
by: Brian Henry | last post by:
Hi, I have a data grid that is set up like this Page items displayed = 10 EnableViewState = false (i dont want to send large amounts of data over the internet!) CustomPaging = false...
3
by: pmud | last post by:
Hi, I have a web page (asp.net, code:c#). I havean html table with text boxes. Based on the user input , records are displayed in the data grid below it. Now the datagrid has a large no. of...
6
by: Alan Silver | last post by:
Hello, I have been playing with the data grid, which looks very powerful for providing paged views of your data, but I don't like the way the footer bits are displayed when there's only one...
6
by: Tejpal Garhwal | last post by:
I have datagrid filled with some data rows. At the run time i want know how many total rows are there in the data grid ? Any idea ? Any Suggestions ? Thanks in advance Tej
0
by: hlam | last post by:
Help - Calculating the total of a column in a data grid -- when data grid is part of Master-Detail set-up I have setup a Master-Detail form using Visual Studio.Net. A ListBox is the (Master)...
0
by: zafar | last post by:
I don't know what property should be used for hiding colums in Data Grid, whereas in Data Grid View we have DataGridView1.Colums(index).Visible = False , But how can I hide Colums in Data Grid.....
1
by: Rob | last post by:
Anyone know of a book or web page that covers them very well ?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.