01.
Imports
System.Data
02.
Imports
System.Data.SqlServerCe
03.
Imports
System.Data.SqlTypes
04.
Imports
System.Drawing
05.
Imports
System.ComponentModel
06.
Imports
System.Windows.Forms
07.
08.
Public
Class
frmHome
09.
10.
Private
Sub
frmHome_Load(
ByVal
sender
As
System.
Object
,
ByVal
e
As
System.EventArgs)
Handles
MyBase
.Load
11.
BindDataGrid()
12.
End
Sub
13.
14.
Private
Sub
BindDataGrid()
15.
16.
Dim
myConnection
As
SqlCeConnection
17.
Dim
dt
As
New
DataTable
18.
Dim
Adapter
As
SqlCeDataAdapter
19.
20.
myConnection =
New
SqlCeConnection(
"Data Source=C:\WindowsFormsApplication\WindowsFormsApplication\Database1.sdf;"
)
21.
myConnection.Open()
22.
Dim
myCommand
As
SqlCeCommand = myConnection.CreateCommand()
23.
myCommand.CommandText =
"SELECT [id], [name], [email] FROM [mytable]"
24.
myCommand.CommandType = CommandType.Text
25.
26.
Adapter =
New
SqlCeDataAdapter(myCommand)
27.
Adapter.Fill(dt)
28.
29.
myConnection.Close()
30.
31.
Me
.dgName.DataSource = dt
32.
33.
Me
.dgName.Columns.Clear()
34.
35.
Dim
column
As
DataGridViewTextBoxColumn
36.
37.
column =
New
DataGridViewTextBoxColumn()
38.
column.DataPropertyName =
"id"
39.
column.HeaderText =
"ID"
40.
column.Width = 50
41.
Me
.dgName.Columns.Add(column)
42.
43.
column =
New
DataGridViewTextBoxColumn()
44.
column.DataPropertyName =
"name"
45.
column.HeaderText =
"Name"
46.
column.Width = 100
47.
Me
.dgName.Columns.Add(column)
48.
49.
column =
New
DataGridViewTextBoxColumn()
50.
column.DataPropertyName =
"email"
51.
column.HeaderText =
"Email"
52.
column.Width = 150
53.
Me
.dgName.Columns.Add(column)
54.
55.
dt =
Nothing
56.
57.
End
Sub
58.
59.
Private
Sub
btnAdd_Click(
ByVal
sender
As
System.
Object
,
ByVal
e
As
System.EventArgs)
Handles
btnAdd.Click
60.
Me
.Hide()
61.
Dim
f
As
New
frmAdd
62.
f.Show()
63.
End
Sub
64.
65.
Private
Sub
btnEdit_Click(
ByVal
sender
As
System.
Object
,
ByVal
e
As
System.EventArgs)
Handles
btnEdit.Click
66.
Me
.Hide()
67.
Dim
f
As
New
frmEdit()
68.
f._strID =
Me
.dgName(0,
Me
.dgName.CurrentCell.RowIndex).Value.ToString()
69.
f.Show()
70.
End
Sub
71.
72.
Private
Sub
btnDel_Click(
ByVal
sender
As
System.
Object
,
ByVal
e
As
System.EventArgs)
Handles
btnDel.Click
73.
If
MessageBox.Show(
"Are you sure to delete?"
,
"Confirm."
, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = DialogResult.Yes
Then
74.
75.
Dim
strID
As
String
=
Me
.dgName(0,
Me
.dgName.CurrentCell.RowIndex).Value.ToString()
76.
77.
Dim
myConnection
As
SqlCeConnection
78.
79.
myConnection =
New
SqlCeConnection(
"Data Source=C:\WindowsFormsApplication\WindowsFormsApplication\Database1.sdf;"
)
80.
myConnection.Open()
81.
Dim
myCommand
As
SqlCeCommand = myConnection.CreateCommand()
82.
myCommand.CommandText =
"DELETE FROM [mytable] WHERE id = '"
& strID &
"'"
83.
myCommand.CommandType = CommandType.Text
84.
myCommand.ExecuteNonQuery()
85.
myConnection.Close()
86.
MessageBox.Show(
"Delete Successfully"
)
87.
88.
BindDataGrid()
89.
End
If
90.
End
Sub
91.
92.
Private
Sub
btnExit_Click(
ByVal
sender
As
System.
Object
,
ByVal
e
As
System.EventArgs)
Handles
btnExit.Click
93.
Me
.Hide()
94.
Dim
f
As
New
frmMain
95.
f.Show()
96.
End
Sub
97.
98.
End
Class