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