已经有很多文章或指南介绍了在
GridView
中使用
Data Source Controls
处理数据,并且也很简单,几乎不要写什么代码,就可以实现很多功能:新增、编辑、删除、分页、排序等等,如果不使用
Data Source Controls
,也能实现这些功能吗?答案是肯定的。本文将演示不使用
Data Source Controls
把
SQL
中的“
Northwind
”数据库中“
Employees
”表中的数据在
GridView
中显示出来。
以下是详细步骤:
1、
从工具箱中拖拽一个
GridView
控件到“设计”页面中,选择“
GridView
任务”下面的“编辑列”,向列中增加三个
BoundFields
和一个
CommandField
,然后结合图
1
修改各自属性,修改后形如图
2
。
图1
:
BoundField | HeaderText | DataField | ReadOnly |
Employee ID | Employee ID | EmployeeID | true |
First Name | First Name | FirstName | false |
Last Name | Last Name | LastName | false |
图2:
2
、更改页面的“
Page_Load()
”方法如下:
protected
void
Page_Load(
object
sender, EventArgs e)
... {
if (!IsPostBack)
...{
BindGrid();
}
}
... {
if (!IsPostBack)
...{
BindGrid();
}
}
3
、在“
Page_Load()
”方法的后面添加
BindGrid()
方法:
private
void
BindGrid()
... {
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("select * from employees", @"data source= .sqlexpress;initial catalog=northwind; integrated security=true");
da.Fill(ds,"employees");
DataView dv = ds.Tables[0].DefaultView;
if (ViewState["sortexpression"] != null)
...{
dv.Sort = ViewState["sortexpression"].ToString() + " " + ViewState["sortdirection"].ToString();
}
GridView1.DataSource=dv;
GridView1.DataBind();
}
... {
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("select * from employees", @"data source= .sqlexpress;initial catalog=northwind; integrated security=true");
da.Fill(ds,"employees");
DataView dv = ds.Tables[0].DefaultView;
if (ViewState["sortexpression"] != null)
...{
dv.Sort = ViewState["sortexpression"].ToString() + " " + ViewState["sortdirection"].ToString();
}
GridView1.DataSource=dv;
GridView1.DataBind();
}
4
、实现分页功能。
按
F4
调出
GridView
的属性页,设置“
AllowPaging
”为
True
,设置“
PageSize
”属性为
3
。
设置“
PageIndexChanging
”事件如下代码:
protected
void
GridView1_PageIndexChanging(
object
sender, GridViewPageEventArgs e)
... {
GridView1.PageIndex = e.NewPageIndex;
BindGrid();
}
... {
GridView1.PageIndex = e.NewPageIndex;
BindGrid();
}
5
、实现排序功能。
选定
GridView
,
选择“
GridView
任务”下面的“编辑列”,设置三个“
BoundFields
”的“
SortExpression
”属性值和“
DataField
”属性相同。
设置
GridView
的“
Sorting
”事件代码如下:
设置GridView的“RowCancelingEdit”事件代码如下:
设置GridView的“ RowUpdating”事件代码如下:
7、保存所有文件。按F5运行一把,效果图如下:
protected
void
GridView1_Sorting(
object
sender, GridViewSortEventArgs e)
... {
ViewState["sortexpression"] = e.SortExpression;
if (ViewState["sortdirection"] == null)
...{
ViewState["sortdirection"] = "asc";
}
else
...{
if (ViewState["sortdirection"].ToString() == "asc")
...{
ViewState["sortdirection"] = "desc";
}
else
...{
ViewState["sortdirection"] = "asc";
}
}
BindGrid();
}
... {
ViewState["sortexpression"] = e.SortExpression;
if (ViewState["sortdirection"] == null)
...{
ViewState["sortdirection"] = "asc";
}
else
...{
if (ViewState["sortdirection"].ToString() == "asc")
...{
ViewState["sortdirection"] = "desc";
}
else
...{
ViewState["sortdirection"] = "asc";
}
}
BindGrid();
}
6
、实现编辑功能。
设置
GridView
的“
RowEditing
”事件代码如下:
protected
void
GridView1_RowEditing(
object
sender, GridViewEditEventArgs e)
... {
GridView1.EditIndex = e.NewEditIndex;
BindGrid();
}
... {
GridView1.EditIndex = e.NewEditIndex;
BindGrid();
}
设置GridView的“RowCancelingEdit”事件代码如下:
protected
void
GridView1_RowCancelingEdit(
object
sender, GridViewCancelEditEventArgs e)
... {
GridView1.EditIndex = -1;
BindGrid();
}
... {
GridView1.EditIndex = -1;
BindGrid();
}
设置GridView的“ RowUpdating”事件代码如下:
protected
void
GridView1_RowUpdating(
object
sender, GridViewUpdateEventArgs e)
... {
int empid;
string fname, lname;
empid = int.Parse(GridView1.Rows[e.RowIndex].
Cells[0].Text);
fname = ((TextBox)GridView1.Rows[e.RowIndex].
Cells[1].Controls[0]).Text;
lname = ((TextBox)GridView1.Rows[e.RowIndex].
Cells[2].Controls[0]).Text;
SqlConnection cnn = new SqlConnection(@"data source= .sqlexpress; initial catalog=northwind; integrated security=true");
cnn.Open();
SqlCommand cmd = new SqlCommand("update employees set firstname=@fname,lastname=@lname where employeeid=@empid",cnn);
cmd.Parameters.Add(new SqlParameter("@fname",fname));
cmd.Parameters.Add(new SqlParameter("@lname", lname));
cmd.Parameters.Add(new SqlParameter("@empid", empid));
cmd.ExecuteNonQuery();
cnn.Close();
GridView1.EditIndex = -1;
BindGrid();
}
... {
int empid;
string fname, lname;
empid = int.Parse(GridView1.Rows[e.RowIndex].
Cells[0].Text);
fname = ((TextBox)GridView1.Rows[e.RowIndex].
Cells[1].Controls[0]).Text;
lname = ((TextBox)GridView1.Rows[e.RowIndex].
Cells[2].Controls[0]).Text;
SqlConnection cnn = new SqlConnection(@"data source= .sqlexpress; initial catalog=northwind; integrated security=true");
cnn.Open();
SqlCommand cmd = new SqlCommand("update employees set firstname=@fname,lastname=@lname where employeeid=@empid",cnn);
cmd.Parameters.Add(new SqlParameter("@fname",fname));
cmd.Parameters.Add(new SqlParameter("@lname", lname));
cmd.Parameters.Add(new SqlParameter("@empid", empid));
cmd.ExecuteNonQuery();
cnn.Close();
GridView1.EditIndex = -1;
BindGrid();
}
7、保存所有文件。按F5运行一把,效果图如下: