列标题单击时排序gridview列 [英] sorting gridview column when column header clicks

查看:77
本文介绍了列标题单击时排序gridview列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,



正在使用asp.net c#和SqlDatabase。



我想要排序gridview列。总计我在gridview中有8列,只有当列标题点击时我才想排序6列。



当列标题点击时,它必须按升序对列进行排序。



请你帮我,怎么做。



谢谢

Hello frnds,

am working on asp.net c# and SqlDatabase.

I want to Sort a gridview columns. Total I have 8 columns in my gridview, only i want to sort 6 columns when column header click.

It must sort the column in ascending order when column header clicks.

Please can you help me, how to do this.

Thanks

推荐答案

将SortExpression添加到您需要排序的字段

例如

ADD SortExpression to the fields you need to sort
for example
<asp:boundfield datafield="ProductCode" headertext="Product Code" xmlns:asp="#unknown">SortExpression="ProductCode" /></asp:boundfield>



i取了我的gridview id = Productlist

那么在C#


i have taken my gridview id =Productlist
THEN IN C#

//sorting Populate and load GridView on Page_Load Event
       private DataTable BindGridView()
       {

           DataTable dtGrid = new DataTable();
           string strSelect = "YOUR SELECT QUERY";
           OleDbCommand cmd = new OleDbCommand(strSelect, con);
           OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd);
           dAdapter.Fill(dtGrid);
           return dtGrid;

       }

       //Create Public Property of SortDirection type and store direction in ViewState.
       public SortDirection dir
       {

           get
           {
               if (ViewState["dirState"] == null)
               {

                   ViewState["dirState"] = SortDirection.Ascending;
               }

               return (SortDirection)ViewState["dirState"];

           }
           set
           {
               ViewState["dirState"] = value;
           }

       }
       //Check Gridview's current direction from ViewState and set new sort direction in Sorting Event.

       protected void gvDetails_Sorting(object sender, GridViewSortEventArgs e)
       {

           string sortingDirection = string.Empty;
           if (dir == SortDirection.Ascending)
           {
               dir = SortDirection.Descending;
               sortingDirection = "Desc";
           }

           else
           {
               dir = SortDirection.Ascending;
               sortingDirection = "Asc";
           }
           DataView sortedView = new DataView(BindGridView());
           sortedView.Sort = e.SortExpression + " " + sortingDirection;
           Productlist.DataSource = sortedView;
           Productlist.DataBind();
       }





快乐编码!! :)



Happy Coding!!:)


关注Link将帮助您解决问题.................





< a href =http://msdn.microsoft.com/en-us/library/ms745786.aspx> http://msdn.microsoft.com/en-us/library/ms745786.aspx [< a href =http://msdn.microsoft.com/en-us/library/ms745786.aspxtarget =_ blanktitle =新窗口> ^ ]
following Link will Help you to resolve your problem.................


http://msdn.microsoft.com/en-us/library/ms745786.aspx[^]


参考此链接



http://stackoverflow.com/questions/702600/sorting-and-paging-with-gridview-asp-net [ ^ ]



< a href =http://forums.asp.net/t/1377779.aspx/1> http://forums.asp.net/t/1377779.aspx/1 [ ^ ]





http://csharpdotnetfreak.blogspot.com/2012/06/sorting-gridview-columns-headers-aspnet.html [ ^ ]



可以帮助你...问候:)
refer this links

http://stackoverflow.com/questions/702600/sorting-and-paging-with-gridview-asp-net[^]

http://forums.asp.net/t/1377779.aspx/1[^]


http://csharpdotnetfreak.blogspot.com/2012/06/sorting-gridview-columns-headers-aspnet.html[^]

may help you...regards :)


这篇关于列标题单击时排序gridview列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆