论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > Asp.net教程
Tag:静态页面,treeview,gridview,repeater,dataset,sqldatareader,ado.net,上传,三层,ajax,xml,留言本,新闻发布,商城,注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,触发器,socket,form认证,登录,视频教程

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

文章类别:Asp.net | 发表日期:2008-10-5 22:13:06

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

视频教程列表
文章教程搜索
 
Asp.net推荐教程
Asp.net热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058