日期:2014-05-18  浏览次数:20851 次

点击gridview排序,不起作用
如题,代码如下
C# code

<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
            CellPadding="3" Font-Size="9pt" HeaderStyle-CssClass="headStyle" OnRowDataBound="GridView1_RowDataBound"
            HorizontalAlign="Center" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None"
            DataKeyNames="DocID" BorderWidth="1px" OnSorting="GridView1_Sorting" Width="100%">


C# code

 #region 点击GridView表头排序

        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            // 从事件参数获取排序数据列
            string sortExpression = e.SortExpression.ToString();
            // 假定为排序方向为“顺序”
            string sortDirection = "ASC";

            // "ASC"与事件参数获取到的排序方向进行比较,进行GridView排序方向参数的修改
            if (sortExpression == this.GridView1.Attributes["SortExpression"])
            {
                //获得下一次的排序状态
                sortDirection = (this.GridView1.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
            }
            // 重新设定GridView排序数据列及排序方向
            this.GridView1.Attributes["SortExpression"] = sortExpression;
            this.GridView1.Attributes["SortDirection"] = sortDirection;
            this.DataBindGrid();
        }

        #endregion
 #region 绑定gridview 数据

        public void DataBindGrid()
        {
            // 获取GridView排序数据列及排序方向
            string sortExpression = this.GridView1.Attributes["SortExpression"];
            string sortDirection = this.GridView1.Attributes["SortDirection"];

            DataTable dtTaa01 = (DataTable)Session["AnyData"];
            GridView1.DataSource = null;

            if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
            {
                dtTaa01.DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
            }
            PagedDataSource pds = new PagedDataSource();
            int cu = dtTaa01.Rows.Count;
            pds.DataSource = dtTaa01.DefaultView;
            pds.AllowPaging = true;
            pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
            pds.PageSize = pageSize;
            AspNetPager1.RecordCount = (int)dtTaa01.Rows.Count;
            if (AspNetPager1.RecordCount < pageSize)
            {
                AspNetPager1.PageSize = AspNetPager1.RecordCount;
            }
            else
            {
                AspNetPager1.PageSize = pageSize;
            }           
            //如果没有数据,则只显示表头和提示信息
            if (dtTaa01.Rows.Count == 0)
            {
                dtTaa01.Rows.Add(dtTaa01.NewRow());
                GridView1.DataSource = pds;
                GridView1.DataBind();
                int columnCount = GridView1.Columns.Count;
                GridView1.Rows[0].Cells.Clear();
                GridView1.Rows[0].Cells.Add(new TableCell());
                GridView1.Rows[0].Cells[0].ColumnSpan = columnCount;
                GridView1.Rows[0].Cells[0].Text = "没有数据";
                GridView1.Rows[0].Cells[0].Style.Add("text-align", "center");
            }
            else
            {
                DataTable mytable = (DataTable)GridView1.DataSource;
                if (mytable != null)
                {
                    DataTable mytablemytable1 = mytable.Clone();
                    mytablemytable1.Clear();