sub showdata()
UseConnectDatabase()
dim ds as new dataset '***
dim dt as new datatable '***
Dim sql As String = "select * from person"
Dim cmd As New MySqlCommand(sql, MyConnect)
adapter = New MySqlDataAdapter(cmd)
'****
if not( ds.tables("alcohol") is nothing) then
ds.tables("alcohol").clear()
end if
'****
adapter.Fill(ds, "alcohol")
dt = ds.tables("alcohol")
AlcoholDataGridView.DataSource = dt
DisconnectDatabase()
end sub
Private Sub DelButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DelButton.Click
UseConnectDatabase()
Dim aliddel As String = Me.AlcoholDataGridView(0, Me.AlcoholDataGridView.CurrentCell.RowIndex).Value.ToString() 'ดึงค่า al_no จาการคลิก
Dim sql As String = "delete from alcohol where al_id='" & aliddel & "'"
'MsgBox(sql)
Dim cmd As New MySqlCommand(sql, MyConnect)
Dim numrows As Integer = cmd.ExecuteNonQuery
showData()
MessageBox.Show("ลบข้อมูลเรียบร้อยแล้ว", "ข้อมูล", MessageBoxButtons.OK, MessageBoxIcon.Information)
'End If
End Sub
dim strIndex as string =""
private sub datagridview1_cellmouseclick(....................
if e.rowindex <> -1 then
with datagridview1
strIndex = .rows(e.rowindex).cells(0).values.tostring()
'or
'textbox1.text = .rows(e.rowindex).cells(0).values.tostring()
end with
end if
end sub
Code (VB.NET)
private sub delbutton_click(........................
connectdatabase()
dim sql as string = "delete from alcohol where '" & strIndex & "'"
'or
'dim sql as string = "delete from alcohol where '" & textbox1.text & "'"
...
...
...
end sub
ตรงนี้เป็นโค้ดตอนที่คลิก datagrid แล้วส่งค่า aliddel ครับเพื่อไปลบครับซึ่งมันก็ลบข้อมูลได้ และกำหนดค่า index สำหรับ binding
แต่พอลบเสร็จมันเออเรอที่ index ดังรูปครับ
Code (VB.NET)
Dim aliddel As String
Dim index As String
Private Sub AlcoholDataGridView_CellMouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles AlcoholDataGridView.CellMouseUp
aliddel= Me.AlcoholDataGridView(0, Me.AlcoholDataGridView.CurrentCell.RowIndex).Value.ToString() 'ดึงค่า al_no จาการคลิก
index = Me.AlcoholDataGridView(0, Me.AlcoholDataGridView.CurrentCell.RowIndex).RowIndex.ToString() 'ดึงค่า index al_no จาการ
Me.BindingContext(data, "alcohol").Position = index ' เออเรอตรงนี้ครับ
End Sub
Private Sub DelButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DelButton.Click
UseConnectDatabase()
Dim sql As String = "delete from alcohol where al_id='" & aliddel & "'"
'MsgBox(sql)
Dim cmd As New MySqlCommand(sql, MyConnect)
Dim numrows As Integer = cmd.ExecuteNonQuery
showData()
MessageBox.Show("ลบข้อมูลเรียบร้อยแล้ว", "ข้อมูล", MessageBoxButtons.OK, MessageBoxIcon.Information)
'End If
End Sub