首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

点击gridview排序,不起作用解决办法

2012-04-19 
点击gridview排序,不起作用如题,代码如下C# codeasp:GridView IDGridView1 runatserver AllowSorti

点击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();                    GridView1.DataSource = mytablemytable1;                }                GridView1.DataSource = pds;                GridView1.DataBind();            }            this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>hiddel12();</script>");        }        #endregion 


Page_Load中
C# code
if (!IsPostBack)     {      Session["AnyData"] = GetGridInTable();      DataBindGrid();     }


[解决办法]
你要学会调试。
先判断是否执行了GridView1_Sorting里面的代码。再看DataBindGrid里面

dtTaa01.DefaultView.Sort
是否正确


[解决办法]
如果你的 代码字段没问题的话,应该是代码顺序问题,也就是说,你的设置的排序位置错误。这样试试



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

PagedDataSource pds = new PagedDataSource();
int cu = dtTaa01.Rows.Count;
pds.DataSource = dtTaa01.DefaultView
dtTaa01.DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);;

热点排行