How to Paging and sorting the gridview

Girdview set in the ui page as below:

  <asp:GridView ID="GridView_Data" AllowPaging="True" AllowSorting="True"
                PageSize="5" runat="server" OnPageIndexChanging="GridView_Data_PageIndexChanging"
         OnRowCreated="GridView_Data_RowCreated" OnSorting="GridView_Data_Sorting">
        </asp:GridView>

 

 

below is the GridView_Data_PageIndexChanging

   protected void GridView_Data_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            Button1_Click(sender,e);//used to select data from database
            GridView_Data.PageIndex = e.NewPageIndex;
            GridView_Data.DataBind();
        }

 

 

the function below is used to select data from the database

protected void Button1_Click(object sender, EventArgs e)
        {
            //Response.Write("<script language='javascript'>alert('ok')</script>");

           // Logger.Info("test log");

   
//log.Debug("Application loaded successfully.");

            System.Configuration.Configuration rootWebConfig =
                System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/MyWebSiteRoot");
            string connString =
                    rootWebConfig.ConnectionStrings.ConnectionStrings["TestConnectionString"].ToString();

            SqlConnection sc = new SqlConnection(connString);
            SqlDataAdapter sa = new SqlDataAdapter("select * from dbo.DatabaseLog", sc);
            ds = new DataSet();
            sa.Fill(ds);
            this.GridView_Data.DataSource = ds;
            GridView_Data.DataBind();        

        }

 

 

 

 

Below are the functions regarding to sorting

 protected string ConvertSortDirectionToSql(SortDirection sortDirection)
        {
            string newSortDirection = String.Empty;

            switch (sortDirection)
            {
                case SortDirection.Ascending:
                    newSortDirection = "ASC";
                    break;

                case SortDirection.Descending:
                    newSortDirection = "DESC";
                    break;
            }

            return newSortDirection;
        }
        protected void Sorting(GridView gv, string sortExpression, SortDirection sortDirection)
        {
           // DataTable dataTable = gv.DataSource as Table;
       DataTable dataTable = (DataTable)ViewState["gvResults_DataSource"];

// ViewState["gvResults_DataSource"] is    container used to store the gv.DataSource  ex: //ViewState["gvResults_DataSource"] = gridview_data.DataSource;
            if (dataTable != null)
            {
                DataView dataView = new DataView(dataTable);
                dataView.Sort = sortExpression + " " + ConvertSortDirectionToSql(sortDirection);

                gv.DataSource = dataView;
                gv.DataBind();
            }

        }
        protected void GridView_Data_Sorting(object sender, GridViewSortEventArgs e)
        {


            Sorting(GridView_Data,e.SortExpression, e.SortDirection);
         
        }

 

Note: Please pay attention to add the function used for selecting data from database, or there won't be any data got in the gridview after select new page index, plus make sure your gridview's datasource is bind as table

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值