Imports System.Data.SqlClient
Public Class FrmAdminCheckMaterialTotal
Const DATABASE_FILE As String = "D:\[AoN]\งาน\ป.ตรี\Project\ProjectMe\ProjectMe\DBmaterialtotal.mdf"
Dim cn As SqlConnection
Dim da As SqlDataAdapter
Dim ds As DataSet
Dim dv As DataView
Dim WithEvents objCurrencyManager As CurrencyManager
Private Sub btnexit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnexit.Click
Me.Hide()
FrmAdminMenu.Show()
End Sub
Private Sub FrmAdminCheckMaterialTotal_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strConnection As String = "Data Source=.\SQLEXPRESS;"
strConnection &= "AttachDbFilename=" & DATABASE_FILE & ";"
strConnection &= "Integrated Security=True;Connect Timeout=30;User Instance=True"
cn = New SqlConnection(strConnection)
Dim strSQL As String = "SELECT * FROM Tablematerialtotal"
da = New SqlDataAdapter
da.SelectCommand = New SqlCommand(strSQL, cn)
ds = New DataSet
Try
da.Fill(ds, "Tablematerialtotal")
Catch ex As SqlException
MsgBox(ex.Message)
Exit Sub
End Try
Dim dtAdmin As DataTable = ds.Tables("Tablematerialtotal")
dv = New DataView(dtAdmin)
txtid.DataBindings.Add("Text", dv, "id")
txtname.DataBindings.Add("Text", dv, "name")
txtin.DataBindings.Add("Text", dv, "input")
txtout.DataBindings.Add("Text", dv, "output")
txttotal.DataBindings.Add("Text", dv, "total")
dgvEmployees.DataSource = dv
objCurrencyManager = Me.BindingContext(dv)
objCurrencyManager_PositionChanged(Nothing, Nothing)
End Sub
Private Sub objCurrencyManager_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles objCurrencyManager.PositionChanged
If objCurrencyManager.Position = 0 Then
btnMoveFirst.Enabled = False
btnMovePrevious.Enabled = False
Else
btnMoveFirst.Enabled = True
btnMovePrevious.Enabled = True
End If
If objCurrencyManager.Position = objCurrencyManager.Count - 1 Then
btnMoveLast.Enabled = False
btnMoveNext.Enabled = False
Else
btnMoveLast.Enabled = True
btnMoveNext.Enabled = True
End If
lblRecordPosition.Text = "เรคอร์ดที่ " & objCurrencyManager.Position + 1 & " จากทั้งหมด " & objCurrencyManager.Count & " เรคอร์ด"
End Sub
Private Sub btnMoveFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMoveFirst.Click
objCurrencyManager.Position = 0
End Sub
Private Sub btnMovePrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMovePrevious.Click
objCurrencyManager.Position -= 1
End Sub
Private Sub btnMoveNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMoveNext.Click
objCurrencyManager.Position += 1
End Sub
Private Sub btnMoveLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMoveLast.Click
objCurrencyManager.Position = objCurrencyManager.Count - 1
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
objCurrencyManager.AddNew()
txtname.Focus()
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
If MsgBox("ต้องการลบเรคอร์ดนี้?", MsgBoxStyle.Question + MsgBoxStyle.YesNo + MsgBoxStyle.DefaultButton2) = MsgBoxResult.Yes Then
objCurrencyManager.RemoveAt(objCurrencyManager.Position)
End If
objCurrencyManager_PositionChanged(Nothing, Nothing)
End Sub
Private Sub btnUpdateDatabase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdateDatabase.Click
Dim tt As Integer
If txtin.Text >= 0 Then
tt = (txtin.Text - txtout.Text)
txttotal.Text = tt
ElseIf txtout.Text > 0 Then
tt = (txtin.Text - txtout.Text)
txttotal.Text = tt
Else
tt = (txtin.Text - txtout.Text)
txttotal.Text = tt
End If
Try
Dim cb As New SqlCommandBuilder(da)
objCurrencyManager.EndCurrentEdit()
Dim num As Integer = da.Update(ds, "Tablematerialtotal")
MsgBox("อัพเดทฐานข้อมูลแล้ว" & vbNewLine & "จำนวนเรคอร์ดที่ถูกอัพเดท: " & num)
Catch ex As SqlException
MsgBox(ex.Message)
End Try
End Sub
End Class