问题描述
我有一些代码,可以从MS SQL Server中的存储过程中加载数据,然后将数据加载到DataGridView
中,效果很好.我想要的是用于连接/加载数据的代码,该代码位于我的Database
类中,然后与DataGridView
相关联的所有内容都存储在我的Form
中,但是我在传递从Database
类转到Form
.
I have a bit of code which loads data from a stored procedure in MS SQL Server and then loads the data into a DataGridView
, which works fine. What I want is for the code that connects / loads the data to sit in my Database
class and then everything associated with the DataGridView
to be stored in my Form
but I am having problems passing the contents of the BindingSource
over to the Form
from the Database
class.
Form1
代码:
Public Class Form1
Dim myDatabaseObj As New Class1()
Dim bindingSource1 As New BindingSource()
Dim connectString As New SqlConnection
Dim objDataAdapter As New SqlDataAdapter
Dim table As New DataTable()
Dim tabletest As New DataTable()
Private Sub loadCompanyList()
Try
Me.dgv_CompanyList.DataSource = Me.bindingSource1
getCompanyList()
Catch ex As NullReferenceException
End Try
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
loadCompanyList()
End Sub
End Class
当我尝试将getCompanyList()
放在类中,然后创建引用Form()
的新对象时,它似乎没有从表中返回任何值到MyForm.BindingSource1.Datasource
,这意味着我的DataGridView
不显示任何数据
When I try place the getCompanyList()
in a class and then create a new object that references the Form()
it does not seem to return any value from the table to the MyForm.BindingSource1.Datasource
meaning my DataGridView
displays no data.
Database
类代码:
.....
Private Sub getCompanyList()
Try
Dim myForm as new Form()
connect_Transaction_Database()
objDataAdapter.SelectCommand = New SqlCommand()
objDataAdapter.SelectCommand.Connection = connectString
objDataAdapter.SelectCommand.CommandText = "sp_GetCompanyList"
objDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure
Dim commandBuilder As New SqlCommandBuilder(Me.objDataAdapter)
Dim table As New DataTable()
table.Locale = System.Globalization.CultureInfo.InvariantCulture
Me.objDataAdapter.Fill(table)
**MyForm.bindingSource1.DataSource = table**
Catch ex As DataException
MsgBox(ex.Message)
Catch ex As NullReferenceException
MsgBox(ex.Message)
End Try
disconnect_Transaction_Database()
End Sub
推荐答案
首先,我会考虑实施使用:
第二,您不需要使用SqlDataAdapter
.相反,您可以使用 SqlCommand 类并将其返回.我还将在方法中包含SqlConnection
,而不是在另一个视图中将其打开和关闭.
Secondly, you don't need to use a SqlDataAdapter
. Instead you can load a DataTable
using the SqlCommand class and return that. I would also contain the SqlConnection
in the method rather than having it opened and closed in a different one.
您的代码应如下所示:
Form1
代码:
Public Class Form1
Private Sub loadCompanyList()
Dim myDatabase As New Database
Me.dgv_CompanyList.DataSource = myDatabase.getCompanyList()
End Sub
End Class
Database
代码:
Public Class Database
Public Function getCompanyList() As DataTable
Dim dt As New DataTable
Using con As New SqlConnection(connectionString),
cmd As New SqlCommand("sp_GetCompanyList", con) With {.CommandType = CommandType.StoredProcedure}
con.Open()
dt.Load(cmd.ExecuteReader())
End Using
Return dt
End Function
End Class
这篇关于将数据绑定到DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!