将数据绑定到DataGridView [英] Binding Data to DataGridView
问题描述
Form1
类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()
尝试
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)
处理MyBase.Load
loadCompanyList()
E nd Sub
结束类
数据库类
当我尝试将 getCompanyList()
放在一个类中,然后创建一个引用 Form()
它似乎没有从表中返回任何值到 MyForm.BindingSource1.Datasource
意味着我的datagridview显示不数据。
.....
Private Sub getCompanyList()
尝试
将myForm作为新的Form()
connect_Transaction_Database()
objDataAdapter.SelectCommand =新建的SqlCommand()
objDataAdapter.SelectCommand.Connection = connectString
objDataAdapter。 SelectCommand.CommandText =sp_GetCompanyList
objDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure
Dim commandBuilder作为新的SqlCommandBuilder(Me.objDataAdapter)
Dim table As New DataTable()
ta ble.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)
结束尝试
disconnect_Transaction_Database()
End Sub
我建议你将GetCompanyList方法转换为返回由数据适配器填充的DataTable的函数。没有什么真正的原因,这个Sub应该具有对表单的依赖,而是形式的方法可以称之为获取DataTable然后执行绑定。
公共函数GetCompanyList()As DataTable
...
...
Dim table As New DataTable()
table.Locale = System.Globalization .CultureInfo.InvariantCulture
Me.objDataAdapter.Fill(table)
返回表
...
...
I have a bit of code which loads data from a stored procedure in MS SQL Server and then loads the data to 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
Database Class
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 not data.
.....
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
I would suggest you make the GetCompanyList method into a Function that returns the DataTable filled by the data adapter. There's no real reason this Sub should have a dependency on the form, instead a method in the form could call this to get the DataTable and then perform the binding.
Public Function GetCompanyList() As DataTable
...
...
Dim table As New DataTable()
table.Locale = System.Globalization.CultureInfo.InvariantCulture
Me.objDataAdapter.Fill(table)
Return table
...
...
这篇关于将数据绑定到DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!