我正在尝试对gridview中的数据进行排序
一切正常但数字列(标记)只对第一个数字进行排序
代码:
protected void gvTrHty_Sorting(object sender, GridViewSortEventArgs e)
{
try
{
this.gviewSorting(e.SortExpression);
}
catch (Exception ex)
{
string arg_15_0 = ex.Message;
}
}
private void gviewSorting(string strSortExp)
{
if (this.ViewState["dvTrain"] == null)
{
DataSet dataSet = this.BindTraining();
dv = dataSet.Tables[0].DefaultView;
}
else
{
DataSet dataSet2 = (DataSet)this.ViewState["dvTrain"];
TrainingHistory.dv = dataSet2.Tables[0].DefaultView;
}
if (TrainingHistory.sortorder)
{
TrainingHistory.sortorder = false;
TrainingHistory.dv.Sort = strSortExp + " DESC";
}
else
{
TrainingHistory.sortorder = true;
TrainingHistory.dv.Sort = strSortExp;
}
this.BindData(TrainingHistory.dv);
}
如果我在gridview中的Mark(列)中有值
Marks----> When I click this for sorting it's taking Marks
1 1
8 1st number only sorted ---> 12
40 21
12 40
21 8
答案 0 :(得分:0)
它将您的“数字”数据视为字符串并对此字符串值进行排序,因此“40”小于“8”。
您的选择是:
实现自己的排序逻辑,如下所示:
public sealed class GenericComparer<T> : IComparer<T>
{
public enum SortOrder
{
Ascending = 0,
Descending = 1
}
private string sortColumn;
private SortOrder sortingOrder;
public string SortColumn
{
get
{
return this.sortColumn;
}
}
public SortOrder SortingOrder
{
get
{
return this.sortingOrder;
}
}
public GenericComparer(string theSortColumn, SortOrder theSortingOrder)
{
this.sortColumn = theSortColumn;
this.sortingOrder = theSortingOrder;
}
public int Compare(T x, T y)
{
PropertyInfo thePropertyInfo = typeof(T).GetProperty(this.sortColumn);
IComparable object1 = (IComparable)thePropertyInfo.GetValue(x, null);
IComparable object2 = (IComparable)thePropertyInfo.GetValue(y, null);
if (this.sortingOrder == SortOrder.Ascending)
{
return object1.CompareTo(object2);
}
else
{
return object2.CompareTo(object1);
}
}
}
现在,在调用.Sort()
方法时,传递此辅助类的新实例(将要排序的列和要排序的方向传递给它 - 升序或降序)。
由于上面的比较器逻辑使用泛型,您可以传递您想要排序的任何类型(即int
,DateTime
,甚至整个域对象。)