นี่โค้ดค่ะ
Private Sub Addbtt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Addbtt.Click
Dim intnumrow As Integer
Dim sqlstr As String = "SELECT Count(*) FROM Employee WHERE IDOfficer = '" & Me.IDOftxt.Text & "'"
cmd = New OleDbCommand(sqlstr, con)
intnumrow = cmd.ExecuteScalar()
If intnumrow > 0 Then
If (affect < 1) Then
MsgBox("เกิดข้อผิดพลาด" & vbNewLine & "ไม่เพิ่มข้อมูลได้")
Else
MsgBox("เพิ่มข้อมูลเรียบร้อยแล้ว")
End If
ClearBindings()
EmpInfo()
If (namebox.Items.Count > 0) Then
namebox.SelectedIndex = 0
End If
End If
End Sub
Public Class DataEmp
Private con As New OleDbConnection(conn)
Private data As DataSet
Private cmd As OleDbCommand
Private adapter As OleDbDataAdapter
Private row_count As Integer = 0
Private _position As Integer = 0
Private Sub DataEmp_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
con.Open()
EmpInfo()
If (namebox.Items.Count > 0) Then
namebox.SelectedIndex = 0
End If
Editbtt.Enabled = False
End Sub
Private Sub namebox_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles namebox.SelectedIndexChanged
empdetial()
End Sub
Private Sub EmpInfo()
'Dim sql As String = "SELECT DISTINCT(EmpFrontName) FROM Employee" ตรวจสอบค่าซ้ำ
Dim sql As String = "SELECT * FROM Employee"
cmd = New OleDbCommand(sql, con)
Dim adapter = New OleDbDataAdapter(cmd)
data = New DataSet()
adapter.Fill(data, "Emp")
namebox.Items.Clear()
For i = 0 To data.Tables("emp").Rows.Count - 1
namebox.Items.Add(data.Tables("emp").Rows(i)("EmpFrontName").ToString())
Next
End Sub
Private Sub empdetial()
Dim sql As String = "SELECT * FROM Employee WHERE EmpFrontName = @Fname"
cmd = New OleDbCommand(sql, con)
cmd.Parameters.AddWithValue("Fname", namebox.SelectedItem)
adapter = New OleDbDataAdapter(cmd)
นี่โค้ดค่ะทั้งหมดค่ะ
Public Class Manageform
Private con As New OleDbConnection(conn)
Private data As DataSet
Private cmd As OleDbCommand
Private Sub ShowData()
con.Open()
Dim sql As String = "SELECT * From EmpPay"
Dim cmd As New OleDbCommand(sql, con)
Dim apater As New OleDbDataAdapter(cmd)
Dim data As New DataSet()
apater.Fill(data, "pay")
Private Sub Manageform_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
con.Close()
End Sub
Private Sub Manageform_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ShowData()
End Sub
Private Sub finebtt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles finebtt.Click
con.Open()
If finetxt.Text = "" Then
MsgBox("กรุณากรอกข้อมูลที่ต้องการค้นหา", MsgBoxStyle.Information)
ElseIf finetxt.Text <> "" Then
Dim sql As String = "SELECT * FROM EmpPay WHERE (IDOfficer Like '%" & finetxt.Text & "%' OR EmpFrontName Like '%" & finetxt.Text & "%' OR EmpNickName Like '%" & finetxt.Text & "%' OR EmpLastName Like '%" & finetxt.Text & "%')"
Dim da As New OleDbDataAdapter(sql, con)
data = New DataSet()
da.Fill(data, "emp")
DataGridView1.DataSource = data.Tables("emp")
Private Sub Delbtt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Delbtt.Click
con.Open()
If MsgBox("ต้องการลบแถวที่เลือกใช่หรือไม่", MsgBoxStyle.OkCancel, MsgBoxResult.Cancel) Then
Return
End If
Dim ID As String = DataGridView1.SelectedRows.Item(0).Cells(0).Value
'Dim ID As String = Me.DataGridView1.CurrentRow.Index
Dim sql As String = "DELETE * FROM EmpPay WHERE IDpayday = @IDpay"
Dim cmd As New OleDbCommand(sql, con)
cmd.Parameters.AddWithValue("IDpay", DataGridView1.SelectedRows)
cmd.ExecuteNonQuery()
ShowData()
Delbtt.Enabled = False
con.Close()
End Sub
Private Sub DataGridView1_RowStateChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowStateChangedEventArgs) Handles DataGridView1.RowStateChanged
If (DataGridView1.SelectedRows.Count > 0) Then
Delbtt.Enabled = True
Else
Delbtt.Enabled = False
End If
End Sub
End Class
Private Sub Delbtt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Delbtt.Click
con.Open()
If MsgBox("ต้องการลบแถวที่เลือกใช่หรือไม่", MsgBoxStyle.OkCancel, MsgBoxResult.Cancel) Then
Return
End If
Dim ID As String = DataGridView1.SelectedRows.Item(0).Cells(0).Value
'Dim ID As String = Me.DataGridView1.CurrentRow.Index
Dim sql As String = "DELETE * FROM EmpPay WHERE IDpayday = @IDpay"
cmd.Parameters.AddWithValue("IDpay", 0)
Dim cmd As New OleDbCommand(sql, con)
For Each dgr As DataGridViewRow in DataGridView1.SelectedRows
cmd.parameters("@IDPay").Value = dgr.Cells(0).Value ' กรณีที่ IDPay อยู่ Column แรกของ Grid นะ (หมายเหตุ: ปกติผมไม่นิยมอ้างอิงถึงข้อมูลด้วยวิธีนี้นะ เพราะมันจะยึดติดเกินไปว่าต้องวาง IDPay เป็น Column แรก)
cmd.ExecuteNonQuery()
Next
ShowData()
Delbtt.Enabled = False
con.Close()
End Sub
Private Sub Delbtt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Delbtt.Click
con.Open()
If MsgBox("ต้องการลบแถวที่เลือกใช่หรือไม่", MsgBoxStyle.OkCancel, MsgBoxResult.Cancel) Then
Return
End If
Dim ID As String = DataGridView1.SelectedRows.Item(0).Cells(0).Value
'Dim ID As String = Me.DataGridView1.CurrentRow.Index
Dim sql As String = "DELETE FROM Payrollday WHERE IDpayday = @IDpay" ' DELETE FROM นะครับ ไม่ใช่ DELETE * FROM ก่อนหน้านี้ผมเขียนผิด
cmd.Parameters.AddWithValue("@IDpay", 0)
Dim cmd As New OleDbCommand(sql, con)
For Each dgr As DataGridViewRow in DataGridView1.SelectedRows
cmd.parameters("@IDPay").Value = dgr.Cells(0).Value ' กรณีที่ IDPayday อยู่ Column แรกของ Grid นะ (หมายเหตุ: ปกติผมไม่นิยมอ้างอิงถึงข้อมูลด้วยวิธีนี้นะ เพราะมันจะยึดติดเกินไปว่าต้องวาง IDPayday เป็น Column แรก)
cmd.ExecuteNonQuery()
Next
ShowData()
Delbtt.Enabled = False
con.Close()
End Sub
Private Sub Delbtt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Delbtt.Click
con.Open()
If MsgBox("ต้องการลบแถวที่เลือกใช่หรือไม่", MsgBoxStyle.OkCancel, MsgBoxResult.Cancel) Then
Return
End If
Dim sql As String = "DELETE FROM Payrollday WHERE IDpayday = @IDpay"
cmd.Parameters.AddWithValue("@IDpay", 0)
Dim cmd As New OleDbCommand(sql, con)
For Each dgr As DataGridViewRow in DataGridView1.SelectedRows
cmd.parameters("@IDPay").Value = dgr.Cells(10).Value
cmd.ExecuteNonQuery()
Next
ShowData()
Delbtt.Enabled = False
con.Close()
End Sub
ถ้ายังไม่ได้
ลองใส่ MessageBox เพิ่มใน For Loop
เผื่อจะได้เงื่อนงำอะไรบ้าง
Code (VB.NET)
For Each dgr As DataGridViewRow in DataGridView1.SelectedRows
cmd.parameters("@IDPay").Value = dgr.Cells(10).Value
dim st As String = "@IDPay = " & dgr.Cells(10).Value.tostring()
Messagebox.Show(st)
cmd.ExecuteNonQuery()
Next
Private Sub Delbtt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Delbtt.Click
con.Open()
'If MsgBox("ต้องการลบแถวที่เลือกใช่หรือไม่", MsgBoxStyle.OkCancel, MsgBoxResult.Cancel) Then
' Return
'End If
Dim sql As String = "DELETE FROM Payrollday WHERE IDpayday = @IDpay"
cmd.Parameters.AddWithValue("@IDpay", 0)
Dim cmd As New OleDbCommand(sql, con)
For Each dgr As DataGridViewRow in DataGridView1.SelectedRows
cmd.parameters("@IDPay").Value = dgr.Cells(10).Value
cmd.ExecuteNonQuery()
Next
ShowData()
Delbtt.Enabled = False
con.Close()
End Sub
Private Sub Delbtt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Delbtt.Click
con.Open()
'If MsgBox("ต้องการลบแถวที่เลือกใช่หรือไม่", MsgBoxStyle.OkCancel, MsgBoxResult.Cancel) Then
' Return
'End If
Dim sql As String = "DELETE FROM Payrollday WHERE IDpayday = @IDpay"
cmd.Parameters.AddWithValue("@IDpay", 0)
Dim cmd As New OleDbCommand(sql, con)
For Each dgr As DataGridViewRow in DataGridView1.SelectedRows
cmd.parameters("@IDPay").Value = dgr.Cells(10).Value
cmd.ExecuteNonQuery()
Next
ShowData()
Delbtt.Enabled = False
con.Close()
End Sub
Private Sub Delbtt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Delbtt.Click
Try
con.Open()
If con.State <> ConnectionState.Open Then
messagebox.Show("ไม่สามารถเปิด Connection" & vbCrLf & con.ConnectionString)
Return
End If
Dim sql As String = "DELETE FROM Payrollday WHERE IDpayday = @IDpay"
cmd.Parameters.AddWithValue("@IDpay", 0)
Dim cmd As New OleDbCommand(sql, con)
For Each dgr As DataGridViewRow in DataGridView1.SelectedRows
cmd.parameters("@IDPay").Value = dgr.Cells(10).Value
cmd.ExecuteNonQuery()
Next
ShowData()
Delbtt.Enabled = False
con.Close()
Catch ex As Exception
Messagebox.Show(ex.Message)
End Try
End Sub
Private Sub Delbtt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Delbtt.Click
Try
If con.State <> ConnectionState.Open Then con.Open()
Dim sql As String = "DELETE FROM Payrollday WHERE IDpayday = @IDpay"
cmd.Parameters.AddWithValue("@IDpay", 0)
Dim cmd As New OleDbCommand(sql, con)
For Each dgr As DataGridViewRow in DataGridView1.SelectedRows
Messagebox.Show("@IDPay=" & dgr.Cells(10).Value.Tostring()) ' ใส่ไว้หน่อยจะได้รู้ว่า ได้ค่า IDpayday ที่ถูกต้อง
cmd.parameters("@IDPay").Value = dgr.Cells(10).Value
cmd.ExecuteNonQuery()
Next
ShowData()
Delbtt.Enabled = False
con.Close()
Catch ex As Exception
Messagebox.Show(ex.Message)
End Try
End Sub