Imports System.Data
Imports System.Data.OleDb
Public Class Form2
Dim conn As String
Dim cn As New OleDbConnection
Dim cm As New OleDbCommand
Dim da As New OleDbDataAdapter
Dim ds As New DataSet
Dim cal As Integer
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
conn = "provider=microsoft.jet.oledb.4.0; data source=c:\infoproductsDB.mdb"
cn.ConnectionString = conn
cm.Connection = cn
End Sub
Private Sub DataGridView_CellMouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView.CellMouseUp
With DataGridView
Try
txtIDType.Text = .Rows.Item(e.RowIndex).Cells("IDType").Value.ToString()
txtID.Text = .Rows.Item(e.RowIndex).Cells("ID").Value.ToString()
txtName.Text = .Rows.Item(e.RowIndex).Cells("Name").Value.ToString()
txtPriceUnit.Text = .Rows.Item(e.RowIndex).Cells("PriceUnit").Value.ToString()
Catch ex As Exception
End Try
End With
End Sub
Public Sub SelectProductType()
DataGridView.Columns.Item(0).HeaderText() = "รหัสสินค้า"
DataGridView.Columns.Item(1).HeaderText() = "รหัสประเภทสินค้า"
DataGridView.Columns.Item(2).HeaderText() = "ชื่อสินค้า"
DataGridView.Columns.Item(3).HeaderText() = "ราคาต่อหน่วย"
End Sub
Private Sub btNovel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btNovel.Click
cm.CommandText = "SELECT * FROM ProductsNovel"
da.SelectCommand = cm
da.Fill(ds, "ProductsNovel")
DataGridView.DataSource = ds.Tables("ProductsNovel")
SelectProductType()
End Sub
Private Sub btSStory_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSStory.Click
cm.CommandText = "SELECT * FROM ProductsShortStory"
da.SelectCommand = cm
da.Fill(ds, "ProductsShortStory")
SelectProductType()
DataGridView.DataSource = ds.Tables("ProductsShortStory")
SelectProductType()
End Sub
Private Sub btCom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btCom.Click
cm.CommandText = "SELECT * FROM ProductsCom"
da.SelectCommand = cm
da.Fill(ds, "ProductsCom")
SelectProductType()
DataGridView.DataSource = ds.Tables("ProductsCom")
SelectProductType()
End Sub
Private Sub btCal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btCal.Click
cal = txtPriceUnit.Text * txtPiece.Text
txtAmount.Text = cal
End Sub
Private Sub btClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btClear.Click
ds.Clear()
End Sub
End Class
แนบรูปให้ดูนะคะ
รบกวนด้วยคะ ต้องแก้ยังไงคะ ขอบคุณมากๆๆคะ
Tag : .NET, Ms Access, Win (Windows App), VB.NET, VS 2008 (.NET 3.x)
Private Sub btNovel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btNovel.Click
DataGridView1.Rows.Clear() ''<======== มั้งนะ
cm.CommandText = "SELECT * FROM ProductsNovel"
da.SelectCommand = cm
da.Fill(ds, "ProductsNovel")
DataGridView.DataSource = ds.Tables("ProductsNovel")
SelectProductType()
End Sub