لاول مربع يضيف المعلومات ويطلع لي رقم ال id --- رقم الاي دي احتاج له في عمليه التحديث
كود :
Private Sub Form_Load(sender As Object, e As EventArgs) Handles MyBase.Load
conx = New SQLiteConnection(String.Format("Data Source= C:\LOG\{0}\DB{1}.s3db", strca, Opencon))
cmdx = New SQLiteCommand("INSERT INTO DBQTC (d1, d2, d3) Values (@d1,@d2,@d3)") With {
.Connection = conx
}
End Sub
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
sender.EndEdit()
If e.ColumnIndex = 3 Then
Dim btn As DataGridViewButtonCell = sender.Item(e.ColumnIndex, e.RowIndex)
If btn.FormattedValue = "Add" Then
Add(sender.CurrentRow)
'btn.Style.NullValue = "Update"
Else
UpdateX1(sender.CurrentRow)
'btn.Style.NullValue = "Add"
End If
End If
End Sub
Private Sub Add(row As DataGridViewRow)
row.Cells(3).Style.NullValue = "Update"
Dim txt1 As String = TryCast(row.Cells(0).Value, String)
Dim txt2 As String = TryCast(row.Cells(1).Value, String)
Dim txt3 As String = TryCast(row.Cells(2).Value, String)
conx.Open()
cmdx.Parameters.AddWithValue("@d1", txt1)
cmdx.Parameters.AddWithValue("@d2", txt2)
cmdx.Parameters.AddWithValue("@d3", txt3)
'------------------------------------
cmdx.CommandText = "SELECT last_insert_rowid()"
NewID = cmdx.ExecuteScalar()
MsgBox(NewID)
MsgBox("Add Row: " & row.Index & vbNewLine &
"txt1: " & txt1 & vbNewLine &
"txt2: " & txt2 & vbNewLine &
"txt3: " & txt3)
conx.Close()
End Sub
Private Sub UpdateX1(row As DataGridViewRow)
row.Cells(3).Style.NullValue = "Add"
Dim txt1 As String = TryCast(row.Cells(0).Value, String)
Dim txt2 As String = TryCast(row.Cells(1).Value, String)
Dim txt3 As String = TryCast(row.Cells(2).Value, String)
Dim UPDATPONT As New SQLiteConnection(String.Format("Data Source= C:\LOG\{0}\DB{1}.s3db", strca, Opencon))
Dim cmdlidXX As New SQLiteCommand("UPDATE DBQTC SET d1=@d1,d2=@d2, d3=@d3 WHERE id=@id ", UPDATPONT)
UPDATPONT.Open()
cmdx.Parameters.AddWithValue("@d1",txt1)
cmdx.Parameters.AddWithValue("@d2", txt1)
cmdx.Parameters.AddWithValue("@d3", txt1)
cmdlidXX.Parameters.AddWithValue("@id", NewID)
cmdlidXX.ExecuteNonQuery()
MsgBox("Update Row: " & row.Index & vbNewLine &
"txt1: " & txt1 & vbNewLine &
"txt2: " & txt2 & vbNewLine &
"txt3: " & txt3)
MsgBox(NewID)
End Sub