Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I created a new project.It has 1 class and 1 form
class "GridView.vb"
form "Form1.vb"

class GridView.vb has following code
VB
Public Shared ConnectionString As String
Public Shared Query As String
Public Shared FormName As String
Public Shared GridViewName As String
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)
    FormName.GridViewName.DataSource = dt
End Function


and Form1.vb has follwoing code into button1
VB
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()


In class this code line is not working
VB
FormName.GridViewName.DataSource = dt


It says GridViewName is not member of string.

Please help me and guide
Posted
Updated 2-Dec-15 1:54am
v4
Comments
StM0n 2-Dec-15 6:43am    
Are you aware of the fact, that <<String>> has no property <<DataSource>>... are you looking for <<GridView.DataSource = dt>>?
Rizwan Chattha 2-Dec-15 6:58am    
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.
Rizwan Chattha 2-Dec-15 6:55am    
Yes i know string does not have property but i don,t know about how to access gridview properties into class. when i write
Form1.DataGridView1.DataSource=dt" it works. but i want to replace Form1 with some variable to use this class in any other projects without changing class again.
Rizwan Chattha 2-Dec-15 6:56am    
"Form1.DataGridView1.DataSource=dt" works but i want to use this class for other projects without any changing in class.

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)
VB.NET
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
VB.NET
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)
C#
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
C#
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
C#
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)
 
Share this answer
 
v3
Comments
Rizwan Chattha 2-Dec-15 8:15am    
I tested below code and it gives error object reference not set to an object.
Dim a As Control
a.Text = a.Parent.Name
MsgBox(a.Text)
CHill60 2-Dec-15 8:28am    
Because you haven't assigned a value to a!
Rizwan Chattha 2-Dec-15 8:32am    
I want to assign currently open form name to a and this code is in class? what should i write in class.
Rizwan Chattha 2-Dec-15 8:28am    
Please provide tested example. I shall be thankfull to you if you provide easy example.
CHill60 2-Dec-15 8:31am    
I've put an easy example in my solution - just substitute your control names for the ones I've used
You try to access the objects by using their names (I hope it's explained good enough).
In your case FormName does NOT Access the Form itself. You have to search in the application.openforms-Collection for the Form-object to get the Form itself, you are using.
With the GridViewName it is the same. Here you have to search in the Form.controls-Collection for the right object and refer to it.

Now you perhaps could acces the objects like you want.

But for me it isn't clear why you want to do it like this. I think that there are easier and better understandable ways to get nearly the same goal ...
 
Share this answer
 
Comments
Rizwan Chattha 2-Dec-15 8:05am    
Purpose is to shorten code and instead of writing code again and again just use class which is made already.
Ralf Meier 2-Dec-15 8:15am    
I understood your Intension - did you understand OOP ?
A Form is a kind of class - you could build it one time like you need it and after this you could instance it (like every control) like you want.

But you could also walk your way. It isn't impossible - but it is not easy.
Did you understood what I have written in my solution ?
Rizwan Chattha 2-Dec-15 8:25am    
If way i am choosing is difficult then what is easy way to acheive goal.
Rizwan Chattha 2-Dec-15 8:17am    
I shall be very thankfull to you if you post easy example which you mentioned above.
I tried application.openforms-collection
but still i am stuck before this i have not used application.openforms-collection.
Please give some example.
Quote:
FormName.GridViewName.DataSource = dt

I guess this should be
VB
DataSource = dt

instead.
 
Share this answer
 
Comments
Rizwan Chattha 2-Dec-15 6:52am    
No it does not works
CPallini 2-Dec-15 6:56am    
You should assign the DataSource properties of the actual GridView object.
Rizwan Chattha 2-Dec-15 6:59am    
"Form1.DataGridView1.DataSource=dt" works but i want to use this class for other projects without any changing in class.
Rizwan Chattha 2-Dec-15 7:00am    
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.
Rizwan Chattha 2-Dec-15 7:02am    
So what changing should i do in class that it shall work for any form name and grid view with any name.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900