Imports System.Data.OleDb
Imports System.Data
Imports System.DateTime
Public Class Form2
Private Sub Form2_Move(sender As Object, e As EventArgs) Handles MyBase.Move
If Not (Me.Location.X = 0 And Me.Location.Y = 0) Then
Me.Location = New Point(0, 0)
End If
End Sub
Dim con As New OleDbConnection
Dim ds As New DataSet
Dim dt As New DataTable
Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
Try
If Not con.State = ConnectionState.Open Then
'open connection
con.Open()
End If
Dim da As New OleDbDataAdapter
Dim dt As New DataTable
Dim ds As New DataSet
da = New OleDbDataAdapter("SELECT * FROM Stock1", con)
da.Fill(dt)
DataGridView2.DataSource = dt.DefaultView
DataGridView2.Columns(0).Visible = True
DataGridView2.Columns(1).Visible = True
DataGridView2.Columns(2).Visible = False
DataGridView2.Columns(3).Visible = False
DataGridView2.Columns(4).Visible = True
DataGridView2.Columns(5).Visible = False
DataGridView2.Columns(6).Visible = False
DataGridView2.Columns.Item(0).Width = 150
DataGridView2.Columns.Item(1).Width = 80
DataGridView2.Columns.Item(4).Width = 80
DataGridView2.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
DataGridView2.Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
DataGridView2.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
DataGridView2.Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
End Try
End Sub
Private Sub RefreshData()
If Not con.State = ConnectionState.Open Then
'open connection
con.Open()
End If
Dim da As New OleDbDataAdapter("SELECT * FROM Tb_History ORDER BY ID", con)
Dim dt As New DataTable
'fill data to datatable
da.Fill(dt)
'offer data in data table into datagridview
Me.DataGridView1.DataSource = dt
'close connection
con.Close()
End Sub
Private Sub add_Click(sender As Object, e As EventArgs) Handles add.Click
Try
Dim cmd As New OleDbCommand
If Not con.State = ConnectionState.Open Then
'open connection
con.Open()
End If
cmd.Connection = con
If semat.Text = "" Then
MessageBox.Show("กรุณาเลือกวัสดุฝึก")
Me.semat.Focus()
Exit Sub
End If
If txtnum.Text = "" Then
MessageBox.Show("กรุณากรอกจำนวนที่เบิก")
Me.txtnum.Focus()
Exit Sub
End If
If txtna1.Text = "" Then
MessageBox.Show("กรุณากรอกชื่อผู้เบิก")
Me.txtna1.Focus()
Exit Sub
End If
If txtna2.Text = "" Then
MessageBox.Show("กรุณากรอกชื่อผู้จ่าย")
Me.txtna2.Focus()
Exit Sub
End If
If txtdate.Text = "" Then
MessageBox.Show("กรุณาเลือกวันเดือนปี")
Me.txtdate.Focus()
Exit Sub
End If
'check whether add new or update
If Me.semat.Tag & "" = "" Then
'add new
'add data to table
cmd.CommandText = " INSERT INTO Tb_History(His_product, His_number, His_student, His_teacher, His_date, His_time)" & _
" VALUES ('" & Me.semat.Text & "'," & Me.txtnum.Text & ",'" & _
Me.txtna1.Text & "','" & Me.txtna2.Text & "','" & Me.txtdate.Text & "','" & _
Me.txttime.Text & "')"
cmd.ExecuteNonQuery()
Else
'update data in table
cmd.CommandText = "Update Tb_History SET " & _
" His_product= '" & Me.semat.Text & "' " & _
" ,His_number=" & Me.txtnum.Text & _
" ,His_student= '" & Me.txtna1.Text & "' " & _
" ,His_teacher= '" & Me.txtna2.Text & "' " & _
" ,His_date= '" & Me.txtdate.Text & "' " & _
" ,His_time= '" & Me.txttime.Text & "' " & _
" WHERE ID_Product= " & Me.semat.Tag
cmd.ExecuteNonQuery()
End If
'refresh data in list
RefreshData()
'close connection
con.Close()
MessageBox.Show("บันทึกลงตารางเรียบร้อยแล้ว")
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
End Try
End Sub
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
con = New OleDbConnection
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\admim\Documents\ฐานข้อมูล5.accdb"
Me.RefreshData()
DataGridView1.Columns.Item(0).Width = 70
DataGridView1.Columns.Item(1).Width = 150
DataGridView1.Columns.Item(2).Width = 90
DataGridView1.Columns.Item(3).Width = 150
DataGridView1.Columns.Item(4).Width = 150
DataGridView1.Columns.Item(5).Width = 90
DataGridView1.Columns.Item(6).Width = 90
DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
DataGridView1.Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
DataGridView1.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
DataGridView1.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
DataGridView1.Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
DataGridView1.Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
DataGridView1.Columns(5).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
DataGridView1.Columns(6).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
End Sub
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
End Sub
Private Sub semat_SelectedIndexChanged(sender As Object, e As EventArgs) Handles semat.SelectedIndexChanged
End Sub
Private Sub txttime_TextChanged(sender As Object, e As EventArgs) Handles txttime.TextChanged
Dim detdate As Date
detdate = Now
txttime.Text = detdate.ToLongTimeString
End Sub
Private Sub clear_Click(sender As Object, e As EventArgs) Handles clear.Click
For Each row As DataGridViewRow In DataGridView1.SelectedRows
If row.Index <> DataGridView1.Rows.Count Then
Me.DataGridView1.SelectedRows(0).Cells(0).Value.ToString()
DataGridView1.Rows.RemoveAt(row.Index)
End If
Next
End Sub
Private Sub delete_Click(sender As Object, e As EventArgs) Handles delete.Click
'check for the selected item in list
If MessageBox.Show("คุณต้องการลบข้อมูลบรรทัดนั้ใช่หรือไม่ ?", "ยืนยันการลบข้อมูล", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
If DataGridView1.Rows.Count > 0 Then
If DataGridView1.SelectedRows.Count > 0 Then
Dim intsid As Integer = Me.DataGridView1.SelectedRows(0).Cells(0).Value
'open connnection
If Not con.State = ConnectionState.Open Then
con.Open()
End If
'delete date
Dim cmd As New OleDbCommand
cmd.Connection = con
cmd.CommandText = "DELETE FROM Tb_History WHERE ID =" & intsid
cmd.ExecuteNonQuery()
'refresh data
Me.RefreshData()
'close connection
con.Close()
MsgBox("ทำการลบข้อมูลเรียบร้อย")
End If
End If
End If
End Sub
Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton2.CheckedChanged
Try
If Not con.State = ConnectionState.Open Then
'open connection
con.Open()
End If
Dim da1 As New OleDbDataAdapter
Dim dt1 As New DataTable
Dim ds1 As New DataSet
da1 = New OleDbDataAdapter("SELECT * FROM Stock2", con)
da1.Fill(dt1)
DataGridView2.DataSource = dt1.DefaultView
DataGridView2.Columns(0).Visible = True
DataGridView2.Columns(1).Visible = True
DataGridView2.Columns(2).Visible = False
DataGridView2.Columns(3).Visible = False
DataGridView2.Columns(4).Visible = True
DataGridView2.Columns(5).Visible = False
DataGridView2.Columns(6).Visible = False
DataGridView2.Columns.Item(0).Width = 150
DataGridView2.Columns.Item(1).Width = 80
DataGridView2.Columns.Item(4).Width = 80
DataGridView2.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
DataGridView2.Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
DataGridView2.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
DataGridView2.Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
End Try
End Sub
Private Sub RadioButton3_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton3.CheckedChanged
Try
If Not con.State = ConnectionState.Open Then
'open connection
con.Open()
End If
Dim da1 As New OleDbDataAdapter
Dim dt1 As New DataTable
Dim ds1 As New DataSet
da1 = New OleDbDataAdapter("SELECT * FROM Stock3", con)
da1.Fill(dt1)
DataGridView2.DataSource = dt1.DefaultView
DataGridView2.Columns(0).Visible = True
DataGridView2.Columns(1).Visible = True
DataGridView2.Columns(2).Visible = False
DataGridView2.Columns(3).Visible = False
DataGridView2.Columns(4).Visible = True
DataGridView2.Columns(5).Visible = False
DataGridView2.Columns(6).Visible = False
DataGridView2.Columns.Item(0).Width = 150
DataGridView2.Columns.Item(1).Width = 80
DataGridView2.Columns.Item(4).Width = 80
DataGridView2.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
DataGridView2.Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
DataGridView2.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
DataGridView2.Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
End Try
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs)
semat.Text = ""
txtnum.Text = ""
txtna1.Text = ""
txtna2.Text = ""
txtdate.Text = ""
txttime.Text = ""
Me.semat.Focus()
End Sub
End Class
Function Reload(TableName As String, OleCon As OleDbConnection) As DataTable
Dim dt As New DataTable
Dim dtap As New OleDbDataAdapter("SELECT * FROM " & TableName, OleCon)
dtap.Fill(dt)
Return dt
End Function
Function Reload1(Stock1 As String, con As OleDbConnection) As DataTable
Dim dt As New DataTable
Dim da As New OleDbDataAdapter("SELECT * FROM " & Stock1, con)
da.Fill(dt)
Return dt
End Function
ลองใช้วิธีนี้ดูคับ เอาโตค้ดชุดนี้ไปวางใน Refresh Data นะคัรบ ท้ายสุด
Code (VB.NET)
If RadioButton3.Checked Then
RadioButton3_CheckedChanged(Nothing, Nothing)
End If
If RadioButton2.Checked Then
RadioButton2_CheckedChanged(Nothing, Nothing)
End If
If RadioButton1.Checked Then
RadioButton1_CheckedChanged(Nothing, Nothing)
End If
Public Function GetTableForm(connectionString As String, _SQL As String) As System.Data.DataTable
Dim ds As New System.Data.DataSet()
Dim conn1 As New System.Data.OleDb.OleDbConnection(connectionString)
conn1.Open()
Dim adapter1 As New System.Data.OleDb.OleDbDataAdapter(_SQL, conn1)
adapter1.Fill(ds)
Return ds.Tables(0)
End Function