栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > asp

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

asp 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

           
          


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

  ///


  /// 返回Data
  ///

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

  }
  ///


  ///从DataSet中除一
  ///

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

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

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

  ///

  ///
  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( "");

  }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/59908.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号