将数据绑定到DataGridView [英] Binding data to a DataGridView

查看:88
本文介绍了将数据绑定到DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码,可以从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

推荐答案

社区用户,所以我想我会提供一个可能对将来有所帮助的答案.

This question has just been bumped by the Community user so I thought I would provide an answer which may help others in the future.

首先,我会考虑实施使用:

.NET Framework垃圾收集器(GC)可以处理托管资源,而无需您进行任何额外的编码.您不需要Managed资源的Using块.但是,您仍然可以使用Using块来强制处置托管资源,而不必等待垃圾收集器.

Managed resources are disposed of by the .NET Framework garbage collector (GC) without any extra coding on your part. You do not need a Using block for managed resources. However, you can still use a Using block to force the disposal of a managed resource instead of waiting for the garbage collector.

第二,您不需要使用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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆