001.
<%@ Page Language=
"VB"
%>
002.
<%@ import
Namespace
=
"System.Data"
%>
003.
<%@ import
Namespace
=
"System.Data.OleDb"
%>
004.
<script runat=
"server"
>
005.
Dim
objConn
As
OleDbConnection
006.
Dim
objCmd
As
OleDbCommand
007.
Dim
strSQL
As
String
008.
009.
Sub
Page_Load(sender
As
Object
, e
As
EventArgs)
010.
Dim
strConnString
As
String
011.
strConnString =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
& _
012.
Server.MapPath(
"database/mydatabase.mdb"
)&
";"
013.
objConn =
New
OleDbConnection(strConnString)
014.
objConn.Open()
015.
016.
IF
Not
Page.IsPostBack()
Then
017.
BindData()
018.
End
IF
019.
End
Sub
020.
021.
Sub
BindData()
022.
strSQL =
"SELECT * FROM customer"
023.
024.
Dim
dtReader
As
OleDbDataReader
025.
objCmd =
New
OleDbCommand(strSQL, objConn)
026.
dtReader = objCmd.ExecuteReader()
027.
028.
029.
myGridView.DataSource = dtReader
030.
myGridView.DataBind()
031.
032.
dtReader.Close()
033.
dtReader =
Nothing
034.
035.
End
Sub
036.
037.
Sub
Page_UnLoad()
038.
objConn.Close()
039.
objConn =
Nothing
040.
End
Sub
041.
042.
Sub
modEditCommand(sender
As
Object
, e
As
GridViewEditEventArgs)
043.
myGridView.EditIndex = e.NewEditIndex
044.
myGridView.ShowFooter =
False
045.
BindData()
046.
End
Sub
047.
048.
Sub
modCancelCommand(sender
As
Object
, e
As
GridViewCancelEditEventArgs)
049.
myGridView.EditIndex = -1
050.
myGridView.ShowFooter =
True
051.
BindData()
052.
End
Sub
053.
054.
Sub
modDeleteCommand(sender
As
Object
, e
As
GridViewDeleteEventArgs)
055.
strSQL =
"DELETE FROM customer WHERE CustomerID = '"
& myGridView.DataKeys.Item(e.RowIndex).Value &
"'"
056.
objCmd =
New
OleDbCommand(strSQL, objConn)
057.
objCmd.ExecuteNonQuery()
058.
059.
myGridView.EditIndex = -1
060.
BindData()
061.
End
Sub
062.
063.
064.
Sub
myGridView_RowCommand(source
As
Object
, e
As
GridViewCommandEventArgs)
065.
If
e.CommandName =
"Add"
Then
066.
067.
Dim
txtCustomerID
As
TextBox =
CType
(myGridView.FooterRow.FindControl(
"txtAddCustomerID"
), TextBox)
068.
069.
Dim
txtName
As
TextBox =
CType
(myGridView.FooterRow.FindControl(
"txtAddName"
), TextBox)
070.
071.
Dim
txtEmail
As
TextBox =
CType
(myGridView.FooterRow.FindControl(
"txtAddEmail"
), TextBox)
072.
073.
Dim
txtCountryCode
As
TextBox =
CType
(myGridView.FooterRow.FindControl(
"txtAddCountryCode"
), TextBox)
074.
075.
Dim
txtBudget
As
TextBox =
CType
(myGridView.FooterRow.FindControl(
"txtAddBudget"
), TextBox)
076.
077.
Dim
txtUsed
As
TextBox =
CType
(myGridView.FooterRow.FindControl(
"txtAddUsed"
), TextBox)
078.
079.
strSQL =
"INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) "
& _
080.
" VALUES ('"
& txtCustomerID.Text &
"','"
& txtName.Text &
"','"
& txtEmail.Text &
"' "
& _
081.
" ,'"
& txtCountryCode.Text &
"','"
& txtBudget.Text &
"','"
& txtUsed.Text &
"') "
082.
objCmd =
New
OleDbCommand(strSQL, objConn)
083.
objCmd.ExecuteNonQuery()
084.
085.
BindData()
086.
End
If
087.
End
Sub
088.
089.
090.
Sub
modUpdateCommand(s
As
Object
, e
As
GridViewUpdateEventArgs)
091.
092.
093.
Dim
txtCustomerID
As
TextBox =
CType
(myGridView.Rows(e.RowIndex).FindControl(
"txtEditCustomerID"
), TextBox)
094.
095.
Dim
txtName
As
TextBox =
CType
(myGridView.Rows(e.RowIndex).FindControl(
"txtEditName"
), TextBox)
096.
097.
Dim
txtEmail
As
TextBox =
CType
(myGridView.Rows(e.RowIndex).FindControl(
"txtEditEmail"
), TextBox)
098.
099.
Dim
txtCountryCode
As
TextBox =
CType
(myGridView.Rows(e.RowIndex).FindControl(
"txtEditCountryCode"
), TextBox)
100.
101.
Dim
txtBudget
As
TextBox =
CType
(myGridView.Rows(e.RowIndex).FindControl(
"txtEditBudget"
), TextBox)
102.
103.
Dim
txtUsed
As
TextBox =
CType
(myGridView.Rows(e.RowIndex).FindControl(
"txtEditUsed"
), TextBox)
104.
105.
strSQL =
"UPDATE customer SET CustomerID = '"
& txtCustomerID.Text &
"' "
& _
106.
" ,Name = '"
& txtName.Text &
"' "
& _
107.
" ,Email = '"
& txtEmail.Text &
"' "
& _
108.
" ,CountryCode = '"
& txtCountryCode.Text &
"' "
& _
109.
" ,Budget = '"
& txtBudget.Text &
"' "
& _
110.
" ,Used = '"
& txtUsed.Text &
"' "
& _
111.
" WHERE CustomerID = '"
& myGridView.DataKeys.Item(e.RowIndex).Value &
"'"
112.
objCmd =
New
OleDbCommand(strSQL, objConn)
113.
objCmd.ExecuteNonQuery()
114.
115.
myGridView.EditIndex = -1
116.
myGridView.ShowFooter =
True
117.
BindData()
118.
End
Sub
119.
120.
</script>
121.
<html>
122.
<head>
123.
<title>ThaiCreate.Com ASP.NET - GridView</title>
124.
</head>
125.
<body>
126.
<form id=
"form1"
runat=
"server"
>
127.
<asp:GridView id=
"myGridView"
runat=
"server"
AutoGenerateColumns=
"False"
128.
ShowFooter=
"True"
129.
DataKeyNames=
"CustomerID"
130.
OnRowEditing=
"modEditCommand"
131.
OnRowCancelingEdit=
"modCancelCommand"
132.
OnRowDeleting=
"modDeleteCommand"
133.
OnRowUpdating=
"modUpdateCommand"
134.
OnRowCommand=
"myGridView_RowCommand"
>
135.
136.
<Columns>
137.
138.
<asp:TemplateField HeaderText=
"CustomerID"
>
139.
<ItemTemplate>
140.
<asp:Label id=
"lblCustomerID"
runat=
"server"
Text=
141.
</ItemTemplate>
142.
<EditItemTemplate>
143.
<asp:TextBox id=
"txtEditCustomerID"
size=
"5"
runat=
"server"
Text=
144.
</EditItemTemplate>
145.
<FooterTemplate>
146.
<asp:TextBox id=
"txtAddCustomerID"
size=
"5"
runat=
"server"
></asp:TextBox>
147.
</FooterTemplate>
148.
</asp:TemplateField>
149.
150.
<asp:TemplateField HeaderText=
"Name"
>
151.
<ItemTemplate>
152.
<asp:Label id=
"lblName"
runat=
"server"
Text=
153.
</ItemTemplate>
154.
<EditItemTemplate>
155.
<asp:TextBox id=
"txtEditName"
size=
"10"
runat=
"server"
Text=
156.
</EditItemTemplate>
157.
<FooterTemplate>
158.
<asp:TextBox id=
"txtAddName"
size=
"10"
runat=
"server"
></asp:TextBox>
159.
</FooterTemplate>
160.
</asp:TemplateField>
161.
162.
<asp:TemplateField HeaderText=
"Email"
>
163.
<ItemTemplate>
164.
<asp:Label id=
"lblEmail"
runat=
"server"
Text=
165.
</ItemTemplate>
166.
<EditItemTemplate>
167.
<asp:TextBox id=
"txtEditEmail"
size=
"20"
runat=
"server"
Text=
168.
</EditItemTemplate>
169.
<FooterTemplate>
170.
<asp:TextBox id=
"txtAddEmail"
size=
"20"
runat=
"server"
></asp:TextBox>
171.
</FooterTemplate>
172.
</asp:TemplateField>
173.
174.
<asp:TemplateField HeaderText=
"CountryCode"
>
175.
<ItemTemplate>
176.
<asp:Label id=
"lblCountryCode"
runat=
"server"
Text=
177.
</ItemTemplate>
178.
<EditItemTemplate>
179.
<asp:TextBox id=
"txtEditCountryCode"
size=
"2"
runat=
"server"
Text=
180.
</EditItemTemplate>
181.
<FooterTemplate>
182.
<asp:TextBox id=
"txtAddCountryCode"
size=
"2"
runat=
"server"
></asp:TextBox>
183.
</FooterTemplate>
184.
</asp:TemplateField>
185.
186.
<asp:TemplateField HeaderText=
"Budget"
>
187.
<ItemTemplate>
188.
<asp:Label id=
"lblBudget"
runat=
"server"
Text=
189.
</ItemTemplate>
190.
<EditItemTemplate>
191.
<asp:TextBox id=
"txtEditBudget"
size=
"6"
runat=
"server"
Text=
192.
</EditItemTemplate>
193.
<FooterTemplate>
194.
<asp:TextBox id=
"txtAddBudget"
size=
"6"
runat=
"server"
></asp:TextBox>
195.
</FooterTemplate>
196.
</asp:TemplateField>
197.
198.
<asp:TemplateField HeaderText=
"Used"
>
199.
<ItemTemplate>
200.
<asp:Label id=
"lblUsed"
runat=
"server"
Text=
201.
</ItemTemplate>
202.
<EditItemTemplate>
203.
<asp:TextBox id=
"txtEditUsed"
size=
"6"
runat=
"server"
Text=
204.
</EditItemTemplate>
205.
<FooterTemplate>
206.
<asp:TextBox id=
"txtAddUsed"
size=
"6"
runat=
"server"
></asp:TextBox>
207.
<asp:Button id=
"btnAdd"
runat=
"server"
Text=
"Add"
CommandName=
"Add"
></asp:Button>
208.
</FooterTemplate>
209.
</asp:TemplateField>
210.
211.
<asp:CommandField ShowEditButton=
"True"
CancelText=
"Cancel"
DeleteText=
"Delete"
EditText=
"Edit"
UpdateText=
"Update"
HeaderText=
"Modify"
/>
212.
<asp:CommandField ShowDeleteButton=
"True"
HeaderText=
"Delete"
/>
213.
214.
</Columns>
215.
</asp:GridView>
216.
</form>
217.
</body>
218.
</html>