DataGrid同时具有分页和排序功能及注意点
当datagrid同时具有分页和排序功能时应注意在重新绑定数据源时,mydatagrid.currentpageindex=0;
下面给实现以上功能的原码,也就不多缀了aspx中包含有datagrid和控制其数据源变化的dropdownlist
datagrid代码
<asp:datagrid id="mydatagrid" runat="server" bordercolor="#cccccc" font-size="100%" horizontalalign="center"
autogeneratecolumns="false" ondeletecommand="mydatagrid_delete" onsortcommand="sort_grid" onpageindexchanged="mydatagrid_pageindexchanged"
datakeyfield="acc_no" pagerstyle-position="bottom" pagerstyle-horizontalalign="center" pagerstyle-mode="nextprev"
pagesize="10" allowsorting="true" allowpaging="true" cellpadding="4" width="100%">
<alternatingitemstyle backcolor="#e9e9e6"></alternatingitemstyle>
<headerstyle font-bold="true" wrap="false" forecolor="white" backcolor="#999999"></headerstyle>
<columns>
<asp:buttoncolumn text="口" commandname="delete"></asp:buttoncolumn>
<asp:boundcolumn datafield="no" sortexpression="no" readonly="true" headertext="序号"></asp:boundcolumn>
<asp:boundcolumn datafield="id" sortexpression="id" headertext="id"></asp:boundcolumn>
<asp:boundcolumn datafield="name" sortexpression="name" headertext="名称"></asp:boundcolumn>
<asp:boundcolumn datafield="c_name" sortexpression="c_name" headertext="各科名称"></asp:boundcolumn>
<asp:boundcolumn datafield="flg" sortexpression="flg" headertext="项目"></asp:boundcolumn>
</columns>
<pagerstyle nextpagetext="下10件" prevpagetext="返回" horizontalalign="center"></pagerstyle>
</asp:datagrid>
dropdownlist代码
<asp:dropdownlist id="ddlwk" runat="server" autopostback="true" enabled="false">
<asp:listitem value="0">东京</asp:listitem>
<asp:listitem value="3">九州</asp:listitem>
<asp:listitem value="8">北海道</asp:listitem>
<asp:listitem value="9">四国</asp:listitem>
</asp:dropdownlist>
aspx.cs文件代码核心如下:
private void page_load(object sender, system.eventargs e)
{
if(!ispostback)
{
session["wp"] ="0";
ddlwk_gets();
bindgrid();
}
}
private void ddlwk_gets()
{
switch (session["wp"].tostring())
{
case "0":ddlwk.selectedindex=0;
break;
case "3":ddlwk.selectedindex=1;
break;
case "8":ddlwk.selectedindex=2;
break;
case "9":ddlwk.selectedindex=3;
break;
default:ddlwk.selectedindex=0;
break;
}
}
protected void bindgrid()
{
mydatagrid.datasource=getdata().tables["vco"].defaultview;
mydatagrid.databind();
//count.text=mydatagrid.columns.count.tostring();
}
/// <summary>
/// 返回data
/// </summary>
///<returns></returns>
private dataset getdata()
{
string strconn=(string) ((namevaluecollection) context.getconfig("system.web/database"))["strconn"];
using (sqlconnection conn = new sqlconnection(strconn))
{
sqlcommand cmd = new sqlcommand("sp_c",conn);
cmd.commandtype=commandtype.storedprocedure;
cmd.parameters.add("@place",sqldbtype.varchar,2);
cmd.parameters["@place"].value=session["wp"].tostring();
conn.open();
sqldataadapter da = new sqldataadapter();
da.selectcommand=cmd;
dataset ds=new dataset();
da.fill(ds,"vco");
count.text="ヒット:"+ds.tables["vco"].rows.count.tostring()+"件";
return ds;
}
}
/// <summary>
///从dataset中除一
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void mydatagrid_delete(object sender, datagridcommandeventargs e)
{
string strid=mydatagrid.datakeys[(int)e.item.itemindex].tostring();
//删除操作
}
/// <summary>
/// 分页操作
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void mydatagrid_pageindexchanged(object source, datagridpagechangedeventargs e)
{
mydatagrid.currentpageindex=e.newpageindex;
bindgrid();
}
/// <summary>
/// 排序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void sort_grid(object sender, datagridsortcommandeventargs e)
{
dataview dv= new dataview(getdata().tables["vco"]);
dv.sort= e.sortexpression.tostring();
mydatagrid.datasource=dv;
mydatagrid.databind();
}
#region web override protected void oninit(eventargs e)
{
// //
initializecomponent();
base.oninit(e);
}
/// <summary> /// </summary>
private void initializecomponent()
{
this.ddlwk.selectedindexchanged += new system.eventhandler(this.ddlwk_selectedindexchanged);
this.load += new system.eventhandler(this.page_load);
}
#endregion
private void ddlwk_selectedindexchanged(object sender, system.eventargs e)
{
session["wp"]=ddlwk.selectedvalue;
mydatagrid.currentpageindex=0;//没有这一句,当该页码超出其他数据源的范围时会出错
bindgrid();
response.write( "<script language='javascript'>parent.menuframe.location.reload();</script>");
}
上一篇: 突然好想我女朋友