01.
using
System;
02.
using
System.Collections.Generic;
03.
using
System.Linq;
04.
using
System.Web;
05.
using
System.Web.UI;
06.
using
System.Web.UI.WebControls;
07.
08.
using
System.Data;
09.
10.
public
partial
class
MyGridView : System.Web.UI.Page
11.
{
12.
protected
void
Page_Init(
object
sender, EventArgs e)
13.
{
14.
GridView1.SelectedIndexChanging +=
new
GridViewSelectEventHandler(GridView1_SelectedIndexChanging);
15.
GridView1.RowDataBound +=
new
GridViewRowEventHandler(GridView1_RowDataBound);
16.
}
17.
18.
protected
void
Page_Load(
object
sender, EventArgs e)
19.
{
20.
GridView1.AutoGenerateColumns =
false
;
21.
GridView1.DataSource = GetPetData();
22.
GridView1.DataKeyNames =
new
string
[] {
"ID"
};
23.
GridView1.DataBind();
24.
}
25.
26.
protected
void
GridView1_RowDataBound(
object
sender, GridViewRowEventArgs e)
27.
{
28.
DataRowView Drv = (DataRowView)e.Row.DataItem;
29.
30.
if
(e.Row.RowType == DataControlRowType.DataRow)
31.
{
32.
e.Row.Cells[1].Text = (e.Row.RowIndex + 1).ToString();
33.
((TextBox)e.Row.FindControl(
"TextBox1"
)).Text = Drv[
"Cost"
].ToString();
34.
}
35.
}
36.
37.
protected
void
GridView1_SelectedIndexChanging(
object
sender, GridViewSelectEventArgs e)
38.
{
39.
40.
41.
}
42.
43.
protected
DataTable GetPetData()
44.
{
45.
46.
string
[] Pet =
new
string
[] {
"Dog"
,
"Cat"
,
"Bird"
,
"Fish"
,
"Dinosaur"
};
47.
int
[] Cost =
new
int
[] { 1000, 1500, 500, 120, 5000 };
48.
49.
DataTable Dt =
new
DataTable();
50.
Dt.Columns.Add(
new
DataColumn(
"ID"
,
typeof
(
int
)));
51.
Dt.Columns.Add(
new
DataColumn(
"Pet"
,
typeof
(
string
)));
52.
Dt.Columns.Add(
new
DataColumn(
"Cost"
,
typeof
(
int
)));
53.
54.
for
(
int
i = 0; i < 5; i++)
55.
{
56.
DataRow Dr = Dt.NewRow();
57.
Dr[
"ID"
] = i + 1;
58.
Dr[
"Pet"
] = Pet[i];
59.
Dr[
"Cost"
] = Cost[i];
60.
Dt.Rows.Add(Dr);
61.
}
62.
63.
return
Dt;
64.
}
65.
}