جرب هذا
أو حسب طلبك
كود :
Imports System.Data.OleDb
Public Class Form1
Dim ConStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\DB.mdb; "
Dim Conn As New OleDbConnection(ConStr)
Dim InfoAdapter As New OleDbDataAdapter("SELECT * FROM Materials ORDER BY Materials_Id", Conn)
Dim InfoTable As New DataTable
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Me.DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
Me.DataGridView1.AllowUserToDeleteRows = False
Me.DataGridView1.AllowUserToAddRows = False
Me.DataGridView1.ReadOnly = True
InfoTable.Clear()
InfoAdapter.Fill(InfoTable)
DataGridView1.DataSource = InfoTable
DataGridView1.ClearSelection()
DataGridView1.Columns(0).HeaderText = "التّرقيم"
DataGridView1.Columns(1).HeaderText = "السلعة"
DataGridView1.Columns(2).HeaderText = "الكمية"
Dim btn As New DataGridViewButtonColumn
btn.HeaderText = "نقل للجدول الاخر"
btn.DefaultCellStyle.NullValue = "نقل"
btn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
DataGridView1.Columns.Add(btn)
End Sub
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
If TypeOf sender.Columns(e.ColumnIndex) Is DataGridViewButtonColumn Then
' قراءة السطر الحالي في الداتاجريدفيو
Dim row As DataGridViewRow = sender.CurrentRow
' إعدادات رسالة التأكيد
Dim msgstyle As MsgBoxStyle = &H180000 + MsgBoxStyle.YesNo + MsgBoxStyle.DefaultButton2 + MsgBoxStyle.Exclamation
Dim msgtext As String = _
"هل أنت متأكّد من أنك تريد حذف هذه السلعة ونقلها لجدول الجرد: ?" & vbNewLine & _
"رقم: " & row.Cells(0).Value & ": " & row.Cells(1).Value & " (كمية: " & row.Cells(2).Value & ")"
' عندما لا يكون الجواب ب(نعم) فقم بالتراجع
If MsgBox(msgtext, msgstyle, "تنبيه") <> MsgBoxResult.Yes Then Return
Try
If Conn.State <> ConnectionState.Open Then Conn.Open()
Dim trans As OleDbTransaction = Conn.BeginTransaction
Dim cmd As New OleDbCommand
cmd.Connection = Conn
cmd.Transaction = trans
cmd.CommandText = "INSERT INTO [Materials_Djerd] ([Djerd_Id], [Djerd_Name], [Djerd_Quantity]) VALUES(?,?,?)"
cmd.Parameters.Clear()
cmd.Parameters.AddWithValue("@Id", row.Cells(0).Value)
cmd.Parameters.AddWithValue("@Name", row.Cells(1).Value)
cmd.Parameters.AddWithValue("@Quantity", row.Cells(2).Value)
If cmd.ExecuteNonQuery > 0 Then ' إذا نجحت الإضافة في الجدول الثاني
cmd.CommandText = "DELETE FROM [Materials] WHERE [Materials_Id]=?"
cmd.Parameters.Clear()
cmd.Parameters.AddWithValue("@Id", row.Cells(0).Value)
If cmd.ExecuteNonQuery() > 0 Then ' إذا نجح الحذف من الجدول الأول
trans.Commit() ' اتمام العمليتين معا النقل والحذف
' إعادة تعبئة الداتاجريدفيو
InfoTable.Clear()
InfoAdapter.Fill(InfoTable)
sender.ClearSelection()
MsgBox("تم النقل")
Else
trans.Rollback() ' التراجع عن العمليتين معا
End If
End If
Catch ex As Exception
MsgBox(ex.Message)
Finally
Conn.Close()
End Try
End If
End Sub
End Classأو حسب طلبك
كود :
If MsgBox(" هل أنت متأكّد من أنك تريد حذف هذه السلعة و نقلها لجدول الجرد : " & Me.DataGridView1.CurrentRow.Cells(1).Value & " ? ", MsgBoxStyle.YesNo + MsgBoxStyle.Critical, "تنبيه") = MsgBoxResult.No Then