1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
| Private Enum StateEnum
StateIdle = 0
StateNew = 1
StateEdit = 2
End Enum
Dim HostConn As New MySqlConnection 'Pour une conexion a base de données MySQL
Dim da As MySqlDataAdapter 'is use to update the dataset and datasource
Dim dst As New DataSet 'miniature of your table - cache table to client
Dim State As StateEnum 'to know if it is in add, edit, or Idle
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
HostConn.ConnectionString = "server=localhost;" _
& "user id=root;" _
& "password=;" _
& "database=personne"
State = StateEnum.StateIdle
Call StateChange()
Try
HostConn.Open()
da = New MySqlDataAdapter("SELECT * FROM cract", HostConn)
da.Fill(dst, "cract")
DataGrid1.DataSource = dst.Tables("cract")
Catch myerror As MySqlException
MessageBox.Show("Error Connecting to Database: " & myerror.Message)
End Try
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
Me.Close()
End Sub
Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
State = StateEnum.StateEdit
Call StateChange()
Me.BindingContext(dst.Tables("cract")).Position = DataGrid1.CurrentRowIndex
txtnom.DataBindings.Add("text", dst.Tables("cract"), "nom")
txtprenom.DataBindings.Add("text", dst.Tables("cract"), "prenom")
txtun.DataBindings.Add("text", dst.Tables("cract"), "user")
txtpass.DataBindings.Add("text", dst.Tables("cract"), "pw")
Call RemoveDataBindings()
DataGrid1.Enabled = True
End Sub
'this procedure is activate the behavior of the controls
Private Sub StateChange()
End Sub
Private Sub RemoveDataBindings()
Dim Ctrl As Control
For Each Ctrl In GroupBox1.Controls
Ctrl.DataBindings.Clear()
Next Ctrl
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
dst.Tables("cract").Rows(DataGrid1.CurrentRowIndex).Delete()
Dim MyCommBuilder As New MySqlCommandBuilder(da)
da.Update(dst, "cract")
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
State = StateEnum.StateNew
Select Case State
Case Is = StateEnum.StateNew 'Ajouter un record
Dim MyNewRow As DataRow = dst.Tables("cract").NewRow
Try
MyNewRow("nom") = txtnom.Text
MyNewRow("prenom") = txtprenom.Text
MyNewRow("user") = txtun.Text
MyNewRow("pw") = txtpass.Text
dst.Tables("cract").Rows.Add(MyNewRow)
Dim MyCommBuild As New MySqlCommandBuilder(da)
da.Update(dst, "cract")
Catch err As Exception
MsgBox(err.Message, MsgBoxStyle.Exclamation, "error")
dst = New DataSet
da = New MySqlDataAdapter("SELECT * FROM cract", HostConn)
da.Fill(dst, "cract")
DataGrid1.DataSource = dst.Tables("cract")
Exit Sub
End Try
Case Is = StateEnum.StateEdit ' Modifier un record
Dim MyEditRow As DataRow = dst.Tables("cract").Rows(DataGrid1.CurrentRowIndex)
MyEditRow.BeginEdit()
MyEditRow("nom") = txtnom.Text
MyEditRow("prenom") = txtprenom.Text
MyEditRow("user") = txtun.Text
MyEditRow("pw") = txtpass.Text
Dim MyCommBuild As New MySqlCommandBuilder(da)
da.Update(dst, "cract")
MsgBox("Record has been updated", MsgBoxStyle.Information)
DataGrid1.Enabled = True
End Select
State = StateEnum.StateIdle
Call StateChange()
End Sub |