无法对gridview列进行排序 [英] unable to sort a gridview column
问题描述
我正在尝试在已生成的gridview中对列'mem_id'进行排序。我没有得到结果。下面是我的代码:
< asp:GridView ID = GridView2 runat = server HorizontalAlign = 中心 AutoGenerateColumns = 错误 allowpaging = True AllowSorting = True
OnSorting = GridView2_Sorting PageSize = 12 onpageindexchanging = GridView2_PageIndexChanging 跨度> <跨度类= 代码属性 > onselectedindexchanged 跨度> <跨度类= 代码关键字 > =跨度> <跨度类= 代码关键字> GridView2_SelectedIndexChanged
style = border -color:#808000;字体大小:小;上:176px;左:27px;位置:绝对;身高:75px;宽度:1350px >
< RowStyle HorizontalAlign = 中心 / >
< 列 >
< asp:BoundField DataField = mem_id H. eaderText = Mem_ID sortexpression = Mem_id / >
< < span class =code-leadattribute> asp:BoundField DataField = mem_name HeaderText = 内存名称 / >
< asp:BoundField DataField = mobile HeaderText = 单元格号: / >
代码落后:
protected void GridView2_Sorting(对象发送者,GridViewSortEventArgs e)
{
的GridView Gridview2 =(GridView的)发送者;
的SqlDataSource DS =(SqlDataSource的)Gridview2.DataSource;
ds.SelectCommand = ds.SelectCommand +order by
+ e.SortExpression;
Gridview2.DataSource = ds;
Gridview2.DataBind();
任何人都可以指导我吗?
为排序类型创建排序属性
public SortDirection sortProperty
{
get
{
if (ViewState [ SortingState] == null )
{
ViewState [ SortingState] = SortDirection.Ascending;
}
return (SortDirection)ViewState [ SortingState跨度>];
}
set
{
ViewState [ SortingState] = value ;
}
}
和排序方法内部
dsProjects = projectDetails.GetProjectHistory(UserName);
if(sortProperty == SortDirection.Ascending)
{
sortProperty = SortDirection.Descending;
sortingDirection =Desc;
}
else
{
sortProperty = SortDirection.Ascending;
sortingDirection =Asc;
}
DataView sortedView = new DataView(dsProjects.Tables [0]);
sortedView.Sort = e.SortExpression ++ sortingDirection;
gvProjectHistory.DataSource = sortedView;
gvProjectHistory.DataBind();
也查看这个链接,它可以帮到你
http://csharpdotnetfreak.blogspot.com/2012/06/sorting- gridview-columns-headers-aspnet.html [ ^ ]
http://asp-net-example.blogspot.in/2008/12/aspnet-gridview-sorting-example-how-to.html [ ^ ]
这是一个非常古老的viewstate方法我有新想法这样做
gridview。 attributes.add(lastsort,asc);
当用户点击行然后g et的属性值是lastsort,它的值是asc
和点击时将属性样式更改为
gridview.attributes.add( lastsort,'desc');
所以第二次你得到desc,反之亦然它对我来说非常好......
I am trying to sort a column 'mem_id 'in already generated gridview. I dont get the result. Hereunder are my codes:
<asp:GridView ID="GridView2" runat="server" HorizontalAlign="Center" AutoGenerateColumns="False" allowpaging="True" AllowSorting="True"
OnSorting="GridView2_Sorting" PageSize="12" onpageindexchanging="GridView2_PageIndexChanging" onselectedindexchanged="GridView2_SelectedIndexChanged"
style="border-color: #808000; font-size:small; top: 176px; left: 27px; position: absolute; height: 75px; width:1350px" >
<RowStyle HorizontalAlign="Center" />
<Columns >
<asp:BoundField DataField="mem_id" HeaderText="Mem_ID" sortexpression="Mem_id"/>
<asp:BoundField DataField="mem_name" HeaderText="Mem Name"/>
<asp:BoundField DataField="mobile" HeaderText="Cell No:"/>
code behind:
protected void GridView2_Sorting(object sender, GridViewSortEventArgs e)
{
GridView Gridview2 = (GridView)sender;
SqlDataSource ds = (SqlDataSource)Gridview2.DataSource;
ds.SelectCommand = ds.SelectCommand + " order by "
+ e.SortExpression;
Gridview2.DataSource = ds;
Gridview2.DataBind();
Can anyone guide me?
Create a sort property for the Sorting type
public SortDirection sortProperty { get { if (ViewState["SortingState"] == null) { ViewState["SortingState"] = SortDirection.Ascending; } return (SortDirection)ViewState["SortingState"]; } set { ViewState["SortingState"] = value; } }
and inside the Sorting method
dsProjects = projectDetails.GetProjectHistory(UserName); if (sortProperty == SortDirection.Ascending) { sortProperty = SortDirection.Descending; sortingDirection = "Desc"; } else { sortProperty = SortDirection.Ascending; sortingDirection = "Asc"; } DataView sortedView = new DataView(dsProjects.Tables[0]); sortedView.Sort = e.SortExpression + " " + sortingDirection; gvProjectHistory.DataSource = sortedView; gvProjectHistory.DataBind();
also check this links, it may helps you
http://csharpdotnetfreak.blogspot.com/2012/06/sorting-gridview-columns-headers-aspnet.html[^]
http://asp-net-example.blogspot.in/2008/12/aspnet-gridview-sorting-example-how-to.html[^]
this is very old method of viewstate i have new idea do this
gridview.attributes.add("lastsort",'asc');
when user click on row then get value of attribute that is lastsort and its value is asc
and when clicked change attribute style to
gridview.attributes.add("lastsort",'desc');
so second time you get desc and vice versa its working excellent for me.....
这篇关于无法对gridview列进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!