Gridview排序与分页-不使用“DataSourceControl DataSource”的情况下如何分页和排序 ......

如果你在GridView控件上设置 AllowPaging="true" or AllowSorting="true" 而没有使用使用数据源控件 DataSource (i.e. SqlDataSource, ObjectDataSource),运行则会出现下列错误:
当你在GridView控件上单击下一页时:
The GridView 'GridViewID' fired event PageIndexChanging which wasn't handled.
当你点击排序时,则回出现:
The GridView 'GridViewID' fired event Sorting which wasn't handled.


不使用“DataSourceControl DataSource”的情况下如何分页和排序 ...

你必须添加一个操作才可以排序及分页。。

public class WebHandler
    {
        private WebHandler() { }
        public static readonly WebHandler Instance = new WebHandler();

        #region GridView Handler

        public string GridViewSortExpression
        {
            get { return HttpContext.Current.Session["SortExpression"] as string ?? string.Empty; }
            set { HttpContext.Current.Session["SortExpression"] = value; }
        }

        public string GridViewSortDirection
        {
            get { return HttpContext.Current.Session["SortDirection"] as string ?? "ASC"; }
            set { HttpContext.Current.Session["SortDirection"] = value; }
        }

        public string GetSortDirection()
        {
            switch (GridViewSortDirection)
            {
                case "ASC":
                    GridViewSortDirection = "DESC";
                    break;
                case "DESC":
                    GridViewSortDirection = "ASC";
                    break;
            }
            return GridViewSortDirection;
        }

        public DataView SortDataTable(DataTable dataTable, bool isPageIndexChanging)
        {
            if (dataTable != null)
            {
                DataView dataView = new DataView(dataTable);
                if (GridViewSortExpression != string.Empty)
                {
                    if (isPageIndexChanging)
                    {
                        dataView.Sort = string.Format("{0} {1}", GridViewSortExpression, GridViewSortDirection);
                    }
                    else
                    {
                        dataView.Sort = string.Format("{0} {1}", GridViewSortExpression, GetSortDirection());
                    }
                }
                return dataView;
            }
            else
            {
                return new DataView();
            }
        }
         
        #endregion
    }

aspx & aspx.cs

<asp:GridView ID="gvInvoiceHistory" runat="server" AllowPaging="True"  OnPageIndexChanging="gvInvoiceHistory_PageIndexChanging" OnRowCreated="gvInvoices_RowCreated" OnSorting="gvInvoiceHistory_Sorting" AllowSorting="True" AutoGenerateColumns="False"  >


//aspx.cs
private readonly WebHandler webHandler = WebHandler.Instance;

protected  void gvInvoiceHistory_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    gvInvoiceHistory.DataSource = webHandler.SortDataTable(GetInvoiceList(), true);
    gvInvoiceHistory.PageIndex = e.NewPageIndex;
    gvInvoiceHistory.DataBind();
}


protected void gvInvoiceHistory_Sorting(object sender, GridViewSortEventArgs e)
{
    webHandler.GridViewSortExpression = e.SortExpression;
    int pageIndex = gvInvoiceHistory.PageIndex;
    gvInvoiceHistory.DataSource = webHandler.SortDataTable(GetInvoiceList(), false);
    gvInvoiceHistory.DataBind();
    gvInvoiceHistory.PageIndex = pageIndex;
}


public virtual DataTable GetInvoiceList()
{
    DataTable result = new DataTable();
    ...........
    return result;
}

 

//DataBind 排序图标丢失,

 

protected virtual void gvInvoices_RowCreated(object sender, GridViewRowEventArgs e)
{ 
    if (e.Row != null && e.Row.RowType == DataControlRowType.Header)
    {
        foreach (TableCell cell in e.Row.Cells)
        {
            if (cell.HasControls())
            {
                LinkButton button = cell.Controls[0] as LinkButton;
                if (button != null)
                {
                    //Label lblsort = new Label(); 
                    if (ddlParentInvoice.SelectedValue != "-1" && webHandler.GridViewSortExpression == button.CommandArgument)
                    {
                        Image image = new Image();
                        if (webHandler.GetSortDirection() == "ASC")
                        {
                            image.SkinID = "SortArrowDown";
                            //lblsort.Text = " <font>▼</font>"; 
                        }
                        else
                        {
                            image.SkinID = "SortArrowUp";
                            //lblsort.Text = " <font>▲</font>";
                        }
                        cell.Controls.Add(image);
                    }
                }
            }
        } 
    } 
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值