在Gridview Asp.net C#中排序 [英] Sorting in Gridview Asp.net C#
问题描述
嗨Frnds,这是Ranjith Rdy。
我的webform上有一个gridview,最后将数据加载到其中。
现在当我点击gridview中的标题列时,我需要对它进行排序。
我有五个字段,我需要使用标题列对它进行排序。
和我给了允许排序= TRUE
这是我的代码。
Hi Frnds, This is Ranjith Rdy.
I have a gridview on my webform and finally loaded data into it.
Now i need to sort it when i click on the header columns in gridview.
I have five fields, I need to SORT it using Header columns.
and i gave Allow Sorting = TRUE
This is my code.
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dtSortTable = GridView1.DataSource as DataTable;
if (dtSortTable != null)
{
DataView dvSortedView = new DataView(dtSortTable);
dvSortedView.Sort = e.SortExpression + "" + getSortDirectionString(e.SortDirection);
GridView1.DataSource = dvSortedView;
GridView1.DataBind();
}
}
private string getSortDirectionString(SortDirection sortDirection)
{
string newSortDirection = String.Empty;
if(sortDirection== SortDirection.Ascending)
{
newSortDirection = "ASC";
}
else
{
newSortDirection = "DESC";
}
return newSortDirection;
}
这是我的上面代码......它无法解决问题。 。
请有人建议我和分拣步骤。
谢谢,
This is my Above code...its unable to work out..
Please anyone can suggest me and steps for SORTING.
Thanks ,
推荐答案
请参考以下关于CP的一些好文章。你肯定会得到一些帮助。
使用对象数据源优化分页和排序 [ ^ ]
使用向上和向下图标排序Gridview,分页 [ ^ ]
GridView:Sorting&使用泛型和Nullable数据类型函数进行分页C#GetValueOrDefault() [ ^ ]
其他一些有用的链接:
Gridview使用C#排序 [ ^ ]
在代码隐藏的ASP.net中为GridView创建排序的代码 [ ^ ]
将GridView绑定到自定义数据对象 [ ^ ]
在Gridview中排序 [ ^ ]
Please refer following some good articles on CP. You will surely get some help from it.
Optimized Paging and Sorting using Object Data Source[^]
Gridview Sorting with Up and Down Icons, Paging[^]
GridView:Sorting & Paging Using Generics And Nullable Datatype Function Of C# GetValueOrDefault()[^]
Some other useful links:
Gridview Sorting Using C#[^]
Code to create Sorting for a GridView in ASP.net in Code Behind[^]
Sorting a GridView Bound to a Custom Data Object[^]
Sorting in Gridview[^]
It看起来你需要
It looks like you need to have
var DataSource = from data in GridViewData
orderby data.Column1
select data;
由于orderby子句采用属性名称。该子句不解析字符串以确定像Dynamic Queryable那样订购什么。
或者,在多个字段上排序的情况下,我会使用
Since the orderby clause takes a property name. The clause doesn't parse the string to determine what to order by like Dynamic Queryable does.
Or, in the case of sorting on multiple fields, I would use
var DataSource = from data in GridViewData
select data;
switch(fieldToSortOn)
{
case "Column1":
DataSource = NewDataSource.OrderBy(x => x.Field1);
break;
case "Column2":
DataSource = NewDataSource.OrderBy(x => x.Field2);
break;
...
}
GridView1.DataSource = DataSource;
GridView1.DataBind();
您也可以尝试使用处理GridView.OnSorting()并使用LINQ动态创建排序表达式 [ ^ ]。
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataTable dt = new DataTable();
dt=getdata();//here getdata() method returns data from database;
Session["data"] = dt;
}
}
protected void ComponentGridView_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dataTable = Session["data"] as DataTable;
if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
dataView.Sort = e.SortExpression + " " + ConvertSortDirection(e.SortDirection);
ComponentGridView.DataSource = dataView;
ComponentGridView.DataBind();
}
}
private string ConvertSortDirection(SortDirection sortDirection)
{
string newSortDirection = String.Empty;
switch (sortDirection)
{
case SortDirection.Ascending:
newSortDirection = "ASC";
break;
case SortDirection.Descending:
newSortDirection = "DESC";
break;
}
return newSortDirection;
}
这篇关于在Gridview Asp.net C#中排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!