请帮助我,因为死线即将结束 [英] Please help me I stucked while dead line is going to be end
问题描述
我创建了一个新项目。它有1个类和1个表单
类GridView.vb
表单Form1.vb
类GridView.vb有以下代码
I created a new project.It has 1 class and 1 form
class "GridView.vb"
form "Form1.vb"
class GridView.vb has following code
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
和Form1.vb将下面的代码放入button1
and Form1.vb has follwoing code into button1
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
FormName.GridViewName.DataSource = dt
它说GridViewName不是字符串的成员。
请帮助我并指导
It says GridViewName is not member of string.
Please help me and guide
推荐答案
根据你的评论
我想让这个类后来成为dll文件。所以后来我不想改变这个类,我也希望在其他项目中使用这个类,其他表单具有不同的表单名称和不同的GridView名称。
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.
你如果你想将它用于任何形式,将需要将控制传递给类方法。
Eg像这样的方法(文本框的简单示例)
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
可以像
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
或者你可以使控件成为类的属性。
注意我使用过Try-Catch(不是我最好的例子) ts use)如果传递的控件没有我想要更新的属性。
- 使用OP的代码(NB未经测试且无检查或错误处理)
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.
- 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表格
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()
在你的GridView.vb中尝试以下
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您调用此表单的表单
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)
你试试使用他们的名字访问对象(我希望它的解释足够好)。
在你的情况下,FormName不会访问表单本身。您必须在application.openforms-Collection中搜索Form-object以获取您正在使用的Form本身。
使用GridViewName它是一样的。在这里,您必须在Form.controls-Collection中搜索正确的对象并参考它。
现在您可以按照自己的意愿访问对象。 />
但对我而言,目前尚不清楚为什么要这样做。我认为有更容易和更好理解的方法来实现几乎相同的目标...
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 ...
FormName.GridViewName.DataSource = dt
FormName.GridViewName.DataSource = dt
我想这应该是
I guess this should be
DataSource = dt
而不是。
instead.
这篇关于请帮助我,因为死线即将结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!