当DataGrid同时具有分页和排序功能时应注意在重新绑定数据源时MyDataGridCurrentPageIndex=;下面给实现以上功能的原码也就不多缀了aspx中包含有DataGrid和控制其数据源变化的dropdownlist DataGrid代码 <asp:datagrid id=MyDataGrid runat=server BorderColor=#CCCCCC FontSize=% HorizontalAlign=Center AutoGenerateColumns=False OnDeleteCommand=MyDataGrid_Delete OnSortCommand=Sort_Grid OnPageIndexChanged=MyDataGrid_PageIndexChanged DataKeyField=ACC_NO PagerStylePosition=Bottom PagerStyleHorizontalAlign=Center PagerStyleMode=NextPrev PageSize= AllowSorting=True AllowPaging=True CellPadding= Width=%> <AlternatingItemStyle BackColor=#EEE></AlternatingItemStyle> <HeaderStyle FontBold=True Wrap=False ForeColor=White BackColor=#></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=下件 PrevPageText=返回 HorizontalAlign=Center></PagerStyle> </asp:datagrid> dropdownlist代码 <asp:dropdownlist id=ddlWk Runat=server AutoPostBack=True Enabled=False> <asp:ListItem Value=>东京</asp:ListItem> <asp:ListItem Value=>九州</asp:ListItem> <asp:ListItem Value=>北海道</asp:ListItem> <asp:ListItem Value=>四国</asp:ListItem> </asp:dropdownlist> aspxcs文件代码核心如下 private void Page_Load(object sender SystemEventArgs e) { if(!IsPostBack) { Session[WP] =; ddlWk_getS(); BindGrid(); } } private void ddlWk_getS() { switch (Session[WP]ToString()) { case :ddlWkSelectedIndex=; break; case :ddlWkSelectedIndex=; break; case :ddlWkSelectedIndex=; break; case :ddlWkSelectedIndex=; break; default:ddlWkSelectedIndex=; break; } } protected void BindGrid() { MyDataGridDataSource=GetData()Tables[vCO]DefaultView; MyDataGridDataBind(); //COUNTText=MyDataGridColumnsCountToString(); } /// <summary> /// 返回Data /// </summary> ///<returns></returns> private DataSet GetData() { string strConn=(String) ((NameValueCollection) ContextGetConfig(systemweb/database))[strConn]; using (SqlConnection conn = new SqlConnection(strConn)) { SqlCommand cmd = new SqlCommand(sp_Cconn); cmdCommandType=CommandTypeStoredProcedure; cmdParametersAdd(@placeSqlDbTypeVarChar); cmdParameters[@place]Value=Session[WP]ToString(); connOpen(); SqlDataAdapter da = new SqlDataAdapter(); daSelectCommand=cmd; DataSet ds=new DataSet(); daFill(dsvCO); CountText=ヒット+dsTables[vCO]RowsCountToString()+件; return ds; } } /// <summary> ///从DataSet中除一 /// </summary> /// <param name=sender></param> /// <param name=E></param> protected void MyDataGrid_Delete(Object sender DataGridCommandEventArgs E) { String strID=MyDataGridDataKeys[(int)EItemItemIndex]ToString(); //删除操作 } /// <summary> /// 分页操作 /// </summary> /// <param name=sender></param> /// <param name=e></param> protected void MyDataGrid_PageIndexChanged(object source DataGridPageChangedEventArgs e) { MyDataGridCurrentPageIndex=eNewPageIndex; 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]); dvSort= eSortExpressionToString(); MyDataGridDataSource=dv; MyDataGridDataBind(); } #region Web override protected void OnInit(EventArgs e) { // // InitializeComponent(); baseOnInit(e); } /// <summary> /// </summary> private void InitializeComponent() { thisddlWkSelectedIndexChanged += new SystemEventHandler(thisddlWk_SelectedIndexChanged); thisLoad += new SystemEventHandler(thisPage_Load); } #endregion private void ddlWk_SelectedIndexChanged(object sender SystemEventArgs e) { Session[WP]=ddlWkSelectedValue; MyDataGridCurrentPageIndex=;//没有这一句当该页码超出其他数据源的范围时会出错 BindGrid(); ResponseWrite( <script language=javascript>nuframelocationreload();</script>); } |