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.
Function
DataTableCountryCode()
As
DataTable
043.
Dim
strConnString
As
String
044.
Dim
dtAdapter
As
OleDbDataAdapter
045.
Dim
dt
As
New
DataTable
046.
047.
strConnString =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
& _
048.
Server.MapPath(
"database/mydatabase.mdb"
)&
";"
049.
objConn =
New
OleDbConnection(strConnString)
050.
objConn.Open()
051.
052.
Dim
strSQL
As
String
053.
strSQL =
"SELECT * FROM country"
054.
055.
dtAdapter =
New
OleDbDataAdapter(strSQL, objConn)
056.
dtAdapter.Fill(dt)
057.
058.
dtAdapter =
Nothing
059.
060.
Return
dt
061.
062.
End
Function
063.
064.
Sub
modEditCommand(sender
As
Object
, e
As
GridViewEditEventArgs)
065.
myGridView.EditIndex = e.NewEditIndex
066.
myGridView.ShowFooter =
False
067.
BindData()
068.
End
Sub
069.
070.
Sub
modCancelCommand(sender
As
Object
, e
As
GridViewCancelEditEventArgs)
071.
myGridView.EditIndex = -1
072.
myGridView.ShowFooter =
True
073.
BindData()
074.
End
Sub
075.
076.
Sub
modDeleteCommand(sender
As
Object
, e
As
GridViewDeleteEventArgs)
077.
strSQL =
"DELETE FROM customer WHERE CustomerID = '"
& myGridView.DataKeys.Item(e.RowIndex).Value &
"'"
078.
objCmd =
New
OleDbCommand(strSQL, objConn)
079.
objCmd.ExecuteNonQuery()
080.
081.
myGridView.EditIndex = -1
082.
BindData()
083.
End
Sub
084.
085.
086.
Sub
myGridView_RowCommand(source
As
Object
, e
As
GridViewCommandEventArgs)
087.
If
e.CommandName =
"Add"
Then
088.
089.
Dim
txtCustomerID
As
TextBox =
CType
(myGridView.FooterRow.FindControl(
"txtAddCustomerID"
), TextBox)
090.
091.
Dim
txtName
As
TextBox =
CType
(myGridView.FooterRow.FindControl(
"txtAddName"
), TextBox)
092.
093.
Dim
txtEmail
As
TextBox =
CType
(myGridView.FooterRow.FindControl(
"txtAddEmail"
), TextBox)
094.
095.
Dim
ddlCountryCode
As
DropDownList =
CType
(myGridView.FooterRow.FindControl(
"ddlAddCountryCode"
), DropDownList)
096.
097.
Dim
txtBudget
As
TextBox =
CType
(myGridView.FooterRow.FindControl(
"txtAddBudget"
), TextBox)
098.
099.
Dim
txtUsed
As
TextBox =
CType
(myGridView.FooterRow.FindControl(
"txtAddUsed"
), TextBox)
100.
101.
strSQL =
"INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) "
& _
102.
" VALUES ('"
& txtCustomerID.Text &
"','"
& txtName.Text &
"','"
& txtEmail.Text &
"' "
& _
103.
" ,'"
& ddlCountryCode.SelectedItem.Value &
"','"
& txtBudget.Text &
"','"
& txtUsed.Text &
"') "
104.
objCmd =
New
OleDbCommand(strSQL, objConn)
105.
objCmd.ExecuteNonQuery()
106.
107.
BindData()
108.
End
If
109.
End
Sub
110.
111.
Sub
myGridView_RowDataBound(source
As
Object
, e
As
GridViewRowEventArgs)
112.
113.
114.
IF e.Row.RowType = DataControlRowType.Footer
Then
115.
116.
117.
Dim
ddlCountryCode
As
DropDownList =
CType
(e.Row.FindControl(
"ddlAddCountryCode"
), DropDownList)
118.
IF
Not
IsNothing(ddlCountryCode)
Then
119.
With
ddlCountryCode
120.
.DataSource = DataTableCountryCode
121.
.DataTextField =
"CountryName"
122.
.DataValueField =
"CountryCode"
123.
.DataBind()
124.
End
With
125.
End
IF
126.
127.
End
IF
128.
129.
130.
IF e.Row.RowType = DataControlRowType.DataRow
Then
131.
132.
133.
Dim
txtCustomerID
As
TextBox =
CType
(e.Row.FindControl(
"txtEditCustomerID"
), TextBox)
134.
IF
Not
IsNothing(txtCustomerID)
Then
135.
txtCustomerID.Text = e.Row.DataItem(
"CustomerID"
)
136.
End
IF
137.
138.
Dim
txtName
As
TextBox =
CType
(e.Row.FindControl(
"txtEditName"
), TextBox)
139.
IF
Not
IsNothing(txtName)
Then
140.
txtName.Text = e.Row.DataItem(
"Name"
)
141.
End
IF
142.
143.
Dim
txtEmail
As
TextBox =
CType
(e.Row.FindControl(
"txtEditEmail"
), TextBox)
144.
IF
Not
IsNothing(txtEmail)
Then
145.
txtEmail.Text = e.Row.DataItem(
"Email"
)
146.
End
IF
147.
148.
Dim
ddlCountryCode
As
DropDownList =
CType
(e.Row.FindControl(
"ddlEditCountryCode"
), DropDownList)
149.
IF
Not
IsNothing(ddlCountryCode)
Then
150.
With
ddlCountryCode
151.
.DataSource = DataTableCountryCode
152.
.DataTextField =
"CountryName"
153.
.DataValueField =
"CountryCode"
154.
.DataBind()
155.
End
With
156.
ddlCountryCode.SelectedIndex = ddlCountryCode.Items.IndexOf(ddlCountryCode.Items.FindByValue(e.Row.DataItem(
"CountryCode"
)))
157.
End
IF
158.
159.
Dim
txtBudget
As
TextBox =
CType
(e.Row.FindControl(
"txtEditBudget"
), TextBox)
160.
IF
Not
IsNothing(txtBudget)
Then
161.
txtBudget.Text = e.Row.DataItem(
"Budget"
)
162.
End
IF
163.
164.
Dim
txtUsed
As
TextBox =
CType
(e.Row.FindControl(
"txtEditUsed"
), TextBox)
165.
IF
Not
IsNothing(txtUsed)
Then
166.
txtUsed.Text = e.Row.DataItem(
"Used"
)
167.
End
IF
168.
169.
End
IF
170.
171.
End
Sub
172.
173.
Sub
modUpdateCommand(s
As
Object
, e
As
GridViewUpdateEventArgs)
174.
175.
176.
Dim
txtCustomerID
As
TextBox =
CType
(myGridView.Rows(e.RowIndex).FindControl(
"txtEditCustomerID"
), TextBox)
177.
178.
Dim
txtName
As
TextBox =
CType
(myGridView.Rows(e.RowIndex).FindControl(
"txtEditName"
), TextBox)
179.
180.
Dim
txtEmail
As
TextBox =
CType
(myGridView.Rows(e.RowIndex).FindControl(
"txtEditEmail"
), TextBox)
181.
182.
Dim
ddlCountryCode
As
DropDownList =
CType
(myGridView.Rows(e.RowIndex).FindControl(
"ddlEditCountryCode"
), DropDownList)
183.
184.
Dim
txtBudget
As
TextBox =
CType
(myGridView.Rows(e.RowIndex).FindControl(
"txtEditBudget"
), TextBox)
185.
186.
Dim
txtUsed
As
TextBox =
CType
(myGridView.Rows(e.RowIndex).FindControl(
"txtEditUsed"
), TextBox)
187.
188.
strSQL =
"UPDATE customer SET CustomerID = '"
& txtCustomerID.Text &
"' "
& _
189.
" ,Name = '"
& txtName.Text &
"' "
& _
190.
" ,Email = '"
& txtEmail.Text &
"' "
& _
191.
" ,CountryCode = '"
& ddlCountryCode.SelectedItem.Value &
"' "
& _
192.
" ,Budget = '"
& txtBudget.Text &
"' "
& _
193.
" ,Used = '"
& txtUsed.Text &
"' "
& _
194.
" WHERE CustomerID = '"
& myGridView.DataKeys.Item(e.RowIndex).Value &
"'"
195.
objCmd =
New
OleDbCommand(strSQL, objConn)
196.
objCmd.ExecuteNonQuery()
197.
198.
myGridView.EditIndex = -1
199.
myGridView.ShowFooter =
True
200.
BindData()
201.
End
Sub
202.
203.
</script>
204.
<html>
205.
<head>
206.
<title>ThaiCreate.Com ASP.NET - GridView</title>
207.
</head>
208.
<body>
209.
<form id=
"form1"
runat=
"server"
>
210.
<asp:GridView id=
"myGridView"
runat=
"server"
AutoGenerateColumns=
"False"
211.
ShowFooter=
"True"
212.
DataKeyNames=
"CustomerID"
213.
OnRowEditing=
"modEditCommand"
214.
OnRowCancelingEdit=
"modCancelCommand"
215.
OnRowDeleting=
"modDeleteCommand"
216.
OnRowUpdating=
"modUpdateCommand"
217.
OnRowCommand=
"myGridView_RowCommand"
218.
OnRowDataBound=
"myGridView_RowDataBound"
>
219.
220.
<Columns>
221.
222.
<asp:TemplateField HeaderText=
"CustomerID"
>
223.
<ItemTemplate>
224.
<asp:Label id=
"lblCustomerID"
runat=
"server"
Text=
225.
</ItemTemplate>
226.
<EditItemTemplate>
227.
<asp:TextBox id=
"txtEditCustomerID"
size=
"5"
runat=
"server"
></asp:TextBox>
228.
</EditItemTemplate>
229.
<FooterTemplate>
230.
<asp:TextBox id=
"txtAddCustomerID"
size=
"5"
runat=
"server"
></asp:TextBox>
231.
</FooterTemplate>
232.
</asp:TemplateField>
233.
234.
<asp:TemplateField HeaderText=
"Name"
>
235.
<ItemTemplate>
236.
<asp:Label id=
"lblName"
runat=
"server"
Text=
237.
</ItemTemplate>
238.
<EditItemTemplate>
239.
<asp:TextBox id=
"txtEditName"
size=
"10"
runat=
"server"
></asp:TextBox>
240.
</EditItemTemplate>
241.
<FooterTemplate>
242.
<asp:TextBox id=
"txtAddName"
size=
"10"
runat=
"server"
></asp:TextBox>
243.
</FooterTemplate>
244.
</asp:TemplateField>
245.
246.
<asp:TemplateField HeaderText=
"Email"
>
247.
<ItemTemplate>
248.
<asp:Label id=
"lblEmail"
runat=
"server"
Text=
249.
</ItemTemplate>
250.
<EditItemTemplate>
251.
<asp:TextBox id=
"txtEditEmail"
size=
"20"
runat=
"server"
></asp:TextBox>
252.
</EditItemTemplate>
253.
<FooterTemplate>
254.
<asp:TextBox id=
"txtAddEmail"
size=
"20"
runat=
"server"
></asp:TextBox>
255.
</FooterTemplate>
256.
</asp:TemplateField>
257.
258.
<asp:TemplateField HeaderText=
"CountryCode"
>
259.
<ItemTemplate>
260.
<asp:Label id=
"lblCountryCode"
runat=
"server"
Text=
261.
</ItemTemplate>
262.
<EditItemTemplate>
263.
<asp:DropDownList id=
"ddlEditCountryCode"
runat=
"server"
></asp:DropDownList>
264.
</EditItemTemplate>
265.
<FooterTemplate>
266.
<asp:DropDownList id=
"ddlAddCountryCode"
runat=
"server"
></asp:DropDownList>
267.
</FooterTemplate>
268.
</asp:TemplateField>
269.
270.
<asp:TemplateField HeaderText=
"Budget"
>
271.
<ItemTemplate>
272.
<asp:Label id=
"lblBudget"
runat=
"server"
Text=
273.
</ItemTemplate>
274.
<EditItemTemplate>
275.
<asp:TextBox id=
"txtEditBudget"
size=
"6"
runat=
"server"
></asp:TextBox>
276.
</EditItemTemplate>
277.
<FooterTemplate>
278.
<asp:TextBox id=
"txtAddBudget"
size=
"6"
runat=
"server"
></asp:TextBox>
279.
</FooterTemplate>
280.
</asp:TemplateField>
281.
282.
<asp:TemplateField HeaderText=
"Used"
>
283.
<ItemTemplate>
284.
<asp:Label id=
"lblUsed"
runat=
"server"
Text=
285.
</ItemTemplate>
286.
<EditItemTemplate>
287.
<asp:TextBox id=
"txtEditUsed"
size=
"6"
runat=
"server"
></asp:TextBox>
288.
</EditItemTemplate>
289.
<FooterTemplate>
290.
<asp:TextBox id=
"txtAddUsed"
size=
"6"
runat=
"server"
></asp:TextBox>
291.
<asp:Button id=
"btnAdd"
runat=
"server"
Text=
"Add"
CommandName=
"Add"
></asp:Button>
292.
</FooterTemplate>
293.
</asp:TemplateField>
294.
295.
<asp:CommandField ShowEditButton=
"True"
CancelText=
"Cancel"
DeleteText=
"Delete"
EditText=
"Edit"
UpdateText=
"Update"
HeaderText=
"Modify"
/>
296.
<asp:CommandField ShowDeleteButton=
"True"
HeaderText=
"Delete"
/>
297.
298.
</Columns>
299.
</asp:GridView>
300.
</form>
301.
</body>
302.
</html>