[原创]DataGrid同时具有分页和排序功能及注意点

当DataGrid同时具有分页和排序功能时应注意在重新绑定数据源时,MyDataGrid.CurrentPageIndex=0;
下面给实现以上功能的原码,也就不多缀了
aspx中包含有DataGrid和控制其数据源变化的dropdownlist
DataGrid代码

 1<asp:datagrid allowpaging="True" allowsorting="True" autogeneratecolumns="False" bordercolor="#CCCCCC" cellpadding="4" datakeyfield="ACC_NO" font-size="100%" horizontalalign="Center" id="MyDataGrid" ondeletecommand="MyDataGrid_Delete" onpageindexchanged="MyDataGrid_PageIndexChanged" onsortcommand="Sort_Grid" pagerstyle-horizontalalign="Center" pagerstyle-mode="NextPrev" pagerstyle-position="Bottom" pagesize="10" runat="server" width="100%">
 2<alternatingitemstyle backcolor="#E9E9E6"></alternatingitemstyle>
 3<headerstyle backcolor="#999999" font-bold="True" forecolor="White" wrap="False"></headerstyle>
 4<columns>
 5<asp:buttoncolumn commandname="Delete" text="口"></asp:buttoncolumn>
 6<asp:boundcolumn datafield="NO" headertext="序号" readonly="True" sortexpression="NO"></asp:boundcolumn>
 7<asp:boundcolumn datafield="ID" headertext="ID" sortexpression="ID"></asp:boundcolumn>
 8<asp:boundcolumn datafield="NAME" headertext="名称" sortexpression="NAME"></asp:boundcolumn>
 9<asp:boundcolumn datafield="C_NAME" headertext="各科名称" sortexpression="C_NAME"></asp:boundcolumn>
10<asp:boundcolumn datafield="FLG" headertext="项目" sortexpression="FLG"></asp:boundcolumn>
11</columns>
12<pagerstyle horizontalalign="Center" nextpagetext="下10件" prevpagetext="返回"></pagerstyle>
13</asp:datagrid>

dropdownlist代码

1<asp:dropdownlist autopostback="True" enabled="False" id="ddlWk" runat="server">
2<asp:listitem value="0">东京</asp:listitem>
3<asp:listitem value="3">九州</asp:listitem>
4<asp:listitem value="8">北海道</asp:listitem>
5<asp:listitem value="9">四国</asp:listitem>
6</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();
}

///

1<summary>   
2/// 返回Data   
3/// </summary>

///

1<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;
}

}
///

1<summary>   
2///从DataSet中除一   
3/// </summary>

///

1<param name="sender"/>

///

1<param name="E"/>

protected void MyDataGrid_Delete(Object sender, DataGridCommandEventArgs E)
{
String strID=MyDataGrid.DataKeys[(int)E.Item.ItemIndex].ToString();
//删除操作
}
///

1<summary>   
2/// 分页操作   
3/// </summary>

///

1<param name="sender"/>

///

1<param name="e"/>

protected void MyDataGrid_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
MyDataGrid.CurrentPageIndex=e.NewPageIndex;
BindGrid();
}
///

1<summary>   
2/// 排序   
3/// </summary>

///

1<param name="sender"/>

///

1<param name="e"/>

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);
}

///

1<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( "

1<script language="javascript">parent.menuframe.location.reload();</script>

");

}

Published At
Categories with Web编程
Tagged with
comments powered by Disqus