001.
<%@ Page Language=
"C#"
Debug=
"true"
%>
002.
<%@ import Namespace=
"System.Data"
%>
003.
<%@ import Namespace=
"System.Data.OleDb"
%>
004.
<script runat=
"server"
>
005.
String strFields =
"CustomerID"
;
006.
void
Page_Load(
object
sender,EventArgs e)
007.
{
008.
if
(!Page.IsPostBack)
009.
{
010.
BindData();
011.
}
012.
}
013.
014.
void
BindData()
015.
{
016.
OleDbConnection objConn =
new
OleDbConnection();
017.
OleDbCommand objCmd =
new
OleDbCommand();
018.
OleDbDataAdapter dtAdapter =
new
OleDbDataAdapter();
019.
DataSet ds =
new
DataSet();
020.
String strConnString,strSQL;
021.
022.
strConnString =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
+
023.
Server.MapPath(
"database/mydatabase.mdb"
) +
";"
;
024.
strSQL =
"SELECT * FROM customer ORDER BY "
+ strFields +
" ASC"
;
025.
026.
objConn.ConnectionString = strConnString;
027.
objCmd.Connection = objConn;
028.
objCmd.CommandText = strSQL;
029.
objCmd.CommandType = CommandType.Text;
030.
031.
dtAdapter.SelectCommand = objCmd;
032.
033.
dtAdapter.Fill(ds);
034.
035.
036.
myGridView.DataSource = ds;
037.
myGridView.DataBind();
038.
039.
dtAdapter =
null
;
040.
objConn.Close();
041.
objConn =
null
;
042.
}
043.
044.
void
SortCommand(Object s, GridViewSortEventArgs e)
045.
{
046.
strFields = e.SortExpression;
047.
BindData();
048.
}
049.
050.
void
myGridView_RowDataBound(Object s, GridViewRowEventArgs e)
051.
{
052.
053.
Label lblCustomerID = (Label)(e.Row.FindControl(
"lblCustomerID"
));
054.
if
(lblCustomerID !=
null
)
055.
{
056.
lblCustomerID.Text = (
string
)DataBinder.Eval(e.Row.DataItem,
"CustomerID"
);
057.
}
058.
059.
060.
Label lblName = (Label)(e.Row.FindControl(
"lblName"
));
061.
if
(lblName !=
null
)
062.
{
063.
lblName.Text = (
string
)DataBinder.Eval(e.Row.DataItem,
"Name"
);
064.
}
065.
066.
067.
Label lblEmail = (Label)(e.Row.FindControl(
"lblEmail"
));
068.
if
(lblEmail !=
null
)
069.
{
070.
lblEmail.Text = (
string
)DataBinder.Eval(e.Row.DataItem,
"Email"
);
071.
}
072.
073.
074.
Label lblCountryCode = (Label)(e.Row.FindControl(
"lblCountryCode"
));
075.
if
(lblCountryCode !=
null
)
076.
{
077.
lblCountryCode.Text = (
string
)DataBinder.Eval(e.Row.DataItem,
"CountryCode"
);
078.
}
079.
080.
081.
Label lblBudget = (Label)(e.Row.FindControl(
"lblBudget"
));
082.
if
(lblBudget !=
null
)
083.
{
084.
lblBudget.Text = DataBinder.Eval(e.Row.DataItem,
"Budget"
).ToString();
085.
}
086.
087.
088.
Label lblUsed = (Label)(e.Row.FindControl(
"lblUsed"
));
089.
if
(lblUsed !=
null
)
090.
{
091.
lblUsed.Text = DataBinder.Eval(e.Row.DataItem,
"Used"
).ToString();
092.
}
093.
}
094.
095.
</script>
096.
<html>
097.
<head>
098.
<title>ThaiCreate.Com ASP.NET - GridView</title>
099.
</head>
100.
<body>
101.
<form id=
"form1"
runat=
"server"
>
102.
<asp:GridView id=
"myGridView"
runat=
"server"
103.
AllowSorting=
"True"
OnSorting=
"SortCommand"
104.
AutoGenerateColumns=
"False"
onRowDataBound=
"myGridView_RowDataBound"
>
105.
<Columns>
106.
107.
<asp:TemplateField SortExpression=
"CustomerID"
HeaderText=
"CustomerID"
>
108.
<ItemTemplate>
109.
<asp:Label id=
"lblCustomerID"
runat=
"server"
></asp:Label>
110.
</ItemTemplate>
111.
</asp:TemplateField>
112.
113.
<asp:TemplateField SortExpression=
"Name"
HeaderText=
"Name"
>
114.
<ItemTemplate>
115.
<asp:Label id=
"lblName"
runat=
"server"
></asp:Label>
116.
</ItemTemplate>
117.
</asp:TemplateField>
118.
119.
<asp:TemplateField SortExpression=
"Email"
HeaderText=
"Email"
>
120.
<ItemTemplate>
121.
<asp:Label id=
"lblEmail"
runat=
"server"
></asp:Label>
122.
</ItemTemplate>
123.
</asp:TemplateField>
124.
125.
<asp:TemplateField SortExpression=
"CountryCode"
HeaderText=
"CountryCode"
>
126.
<ItemTemplate>
127.
<asp:Label id=
"lblCountryCode"
runat=
"server"
></asp:Label>
128.
</ItemTemplate>
129.
</asp:TemplateField>
130.
131.
<asp:TemplateField SortExpression=
"Budget"
HeaderText=
"Budget"
>
132.
<ItemTemplate>
133.
<asp:Label id=
"lblBudget"
runat=
"server"
></asp:Label>
134.
</ItemTemplate>
135.
</asp:TemplateField>
136.
137.
<asp:TemplateField SortExpression=
"Used"
HeaderText=
"Used"
>
138.
<ItemTemplate>
139.
<asp:Label id=
"lblUsed"
runat=
"server"
></asp:Label>
140.
</ItemTemplate>
141.
</asp:TemplateField>
142.
143.
</Columns>
144.
</asp:GridView>
145.
</form>
146.
</body>
147.
</html>