Based on your comment
Quote:
I want to make this class later dll file. So later i don,t want to change this class and i also want to use this class in other projects with many other forms with different form names and also with different GridView names.
You will need to pass the control through to the class method if you want to use it for any form.
E.g. a method like this (simple example for text box)
Public Sub UpdateControl(ByVal cc As Control)
If cc Is Nothing Then
MessageBox.Show("Form or control not defined")
Return
End If
Try
cc.Text = cc.Parent.Name
Catch ex As Exception
MessageBox.Show("Error {0}", ex.Message)
End Try
End Sub
could be called like
Private Sub btnForm2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnForm2.Click
myC.UpdateControl(f2.Controls.Find("tbForm2", True)(0))
End Sub
Or you could make the control a property of the class.
Note I've used Try-Catch (not the best example of its use) in case the control passed does not have the property I'm trying to update.
[EDIT] - using the OP's code (NB untested and no checking or error handling)
Public Shared ConnectionString As String
Public Shared Query As String
Public Shared FormName As String
Public Shared GridViewName As String
Public Shared GridView as Control
Public Shared Function ExecuteQuery()
Dim con As New SqlConnection
con.ConnectionString = ConnectionString
con.Open()
Dim da As SqlDataAdapter = New SqlDataAdapter(Query, con)
Dim dt As New DataTable
da.Fill(dt)
GridView.DataSource = dt
End Function
And in the form
GridView.FormName = Me.Text & ".vb"
GridView.GridViewName = "DataGridView1"
GridView.ConnectionString = "Server = CHATTHA; Database = Test; User Id = sa; Password = pak123"
GridView.Query = "Select * from Table1"
GridView.GridView = Me.Controls.Find("DataGridView1", True)(0)
GridView.ExecuteQuery()
[EDIT 2 - OP is struggling...]
In your GridView.vb try the following
Public Shared ConnectionString As String
Public Shared Query As String
Public Shared FormName As String
Public Shared GridViewName As String
Public Shared Function ExecuteQuery(GridView as DataGridView)
Dim con As New SqlConnection
con.ConnectionString = ConnectionString
con.Open()
Dim da As SqlDataAdapter = New SqlDataAdapter(Query, con)
Dim dt As New DataTable
da.Fill(dt)
GridView.DataSource = dt
End Function
And on your form where you are calling this put
GridView.FormName = Me.Text & ".vb"
GridView.GridViewName = "DataGridView1"
GridView.ConnectionString = "Server = CHATTHA; Database = Test; User Id = sa; Password = pak123"
GridView.Query = "Select * from Table1"
GridView.ExecuteQuery(Me.DataGridView1)