Datagrid and database
I have a form consisting of datagridview and two buttons of update and load all.. I have used fillby method to obtain data from database. The query seems to be working fine, except that the data is not shown in the gridview.
As in, for the query, if the returned rows are 2, it will show 3 rows in the gridview, but all empty.
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.L开发者_如何学编程oad
'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
you should add dgv1.DataSource = bs
on every button events so that the data on the GridView will update
精彩评论