数据网格和数据库

时间:2011-10-07 06:53:02

标签: vb.net datagridview

我有一个由datagridview和两个更新和加载按钮组成的表单。我使用了fillby方法从数据库中获取数据。查询似乎工作正常,但数据未显示在gridview中。

对于查询,如果返回的行为2,它将在gridview中显示3行,但全部为空。

Imports System.Data
Imports System.Data.OleDb
Imports System.EventArgs
Imports System.Data.OleDb.OleDbConnection
Imports System.Data.OleDb.OleDbCommand

Public Class Form1
    Inherits System.Windows.Forms.Form

    Dim wrkdir As String = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly.Location())
    Dim da As New OleDbDataAdapter
    Dim ds As New DataSet
    Dim bs As New BindingSource
    Dim edit As Boolean
    'Dim cnn As OleDbConnection


    Dim cnn As New OleDbConnection("Provider=microsoft.jet.oledb.4.0;Data Source=E:\Project-Hemtech\HemDatabase1.mdb;")

    'cnn = New OleDbConnection("Provider=microsoft.jet.oledb.4.0;Data Source=E:\Project-Hemtech\HemDatabase1.mdb;")
    'cnn.Open()

    Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click

        If edit Then
            da.Update(ds, "partno")
            edit = False
        End If

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'HemDatabase1DataSet.partno' table. You can move, or remove it, as needed.
        Me.PartnoTableAdapter.Fill(Me.HemDatabase1DataSet.partno)

        dgv1.DataSource = bs

        'Dim cnn As New OleDbConnection("Provider=microsoft.jet.oledb.4.0;Data Source=E:\Project-Hemtech\HemDatabase1.mdb;")
        'cnn = New OleDbConnection("Provider=microsoft.jet.oledb.4.0;Data Source=E:\Project-Hemtech\HemDatabase1.mdb;")
        'cnn.Open()

    End Sub

    Private Sub button2_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click

        ds.Tables.Clear()

        If tsText.Text <> "" Then

            Dim sql As String = "SELECT partno.partnum, partno.partname, partno.partdesc, partno.partqty " & _
                                "FROM(partno)" & _
                                "WHERE (((partno.type)='" & tsText.Text & "'));"

            Dim cmd As New OleDbCommand(sql, cnn)

            da.SelectCommand = cmd

            Dim cmdBuilder As New OleDbCommandBuilder(da)

            da.Fill(ds, "partno")
            bs.DataSource = ds.Tables(0)

        Else

            Exit Sub

        End If
    End Sub

    Private Sub button3_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles button3.Click

        ds.Tables.Clear()

        Dim sql As String = "SELECT * FROM partno;"
        Dim cmd As New OleDbCommand(sql, cnn)

        da.SelectCommand = cmd

        Dim cmdbuilder As New OleDbCommandBuilder(da)
        da.Fill(ds, "partno")
        bs.DataSource = ds.Tables(0)

    End Sub

    Private Sub FillByToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button3.Click
        Try
            Me.PartnoTableAdapter.FillBy(Me.HemDatabase1DataSet.partno, tsText.Text)
        Catch ex As System.Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        End Try

    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

您应该在每个按钮事件上添加dgv1.DataSource = bs,以便GridView上的数据更新