在asp.net 2.0下,gridview是十分方便的了,加一个datasource系列的控件的话,就可以马上和gridview绑定,十分方便。但其实也可以
使用datatable或者dataview的,这个时候就不是用datasource系列控件了。下面讲下如何在asp.net 2.0下,实现gridview控件的翻页,各列排序,
编辑的功能。
首先,我们读取的是northwind数据库中的employee表。放置一个gridview后,添加几个绑定的列,代码如下
<asp:gridview id="gridview1" runat="server" allowpaging="true" allowsorting="true"
autogeneratecolumns="false" cellpadding="4" forecolor="#333333" gridlines="none"
width="100%" datakeynames="employeeid" onpageindexchanging="gridview1_pageindexchanging" onrowediting="gridview1_rowediting" onrowupdating="gridview1_rowupdating" onsorting="gridview1_sorting" pagesize="3" onrowcancelingedit="gridview1_rowcancelingedit" onrowcommand="gridview1_rowcommand">
<footerstyle backcolor="#990000" font-bold="true" forecolor="white" />
<columns>
<asp:boundfield datafield="employeeid" headertext="employee id" readonly="true" />
<asp:boundfield datafield="firstname" headertext="first name" sortexpression="firstname" />
<asp:boundfield datafield="lastname" headertext="last name" sortexpression="lastname" />
<asp:commandfield showeditbutton="true" />
</columns>
<rowstyle backcolor="#fffbd6" forecolor="#333333" />
<selectedrowstyle backcolor="#ffcc66" font-bold="true" forecolor="navy" />
<pagerstyle backcolor="#ffcc66" forecolor="#333333" horizontalalign="center" />
<headerstyle backcolor="#990000" font-bold="true" forecolor="white" />
<alternatingrowstyle backcolor="white" />
</asp:gridview>
首先,我们要实现分页,把allowpaging设置为true,并设置每页的分页条数,最后在codebehind中写入
protected void gridview1_pageindexchanging(object sender, gridviewpageeventargs e)
{
gridview1.pageindex = e.newpageindex;
bindgrid();
}
为了实现每列都可以自动点击排序,可以设置allowsorting=true,然后设置onsorting="gridview1_sorting",其中gridview_sorting
代码为
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();
}
很明显,设置viewsate来保存每次排序时的顺序,上面的相信很容易理解。
最后,实现编辑功能,因为在aspx页面中已经设置了onrowediting="gridview1_rowediting",其中gridview1_rowediting的代码为
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=localhost;initial catalog=northwind;user id=sa;password=123456");
cnn.open();
sqlcommand cmd = new sqlcommand("update employees set [email protected],[email protected] where [email protected]", 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();
}
protected void gridview1_rowediting(object sender, gridviewediteventargs e)
{
gridview1.editindex = e.neweditindex;
bindgrid();
}
protected void gridview1_rowcancelingedit(object sender, gridviewcancelediteventargs e)
{
gridview1.editindex = -1;
bindgrid();
}
可以看到,上面的代码和asp.net 1.1版本的其实原理是差不多的。最后,bindgrid()的过程很简单,为绑定咯
dataset ds = new dataset();
sqldataadapter da = new sqldataadapter("select * from employees", @"data source=localhost;initial catalog=northwind;user id=sa;password=123456");
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();
这里gridview绑定的是dataview,并且用dv.sort设定了每次排序的顺序,也就是说,每次排序后其顺序都是保持不变的。
当然最后是page_load事件咯
protected void page_load(object sender, eventargs e)
{
if(!ispostback)
{
bindgrid();
}
}
新闻热点
疑难解答
图片精选