按字符串格式排序列未正确显示. [英] Sorting on String format Column not Appeared properly.
问题描述
在Loan_number列之间具有各种列的网格视图为字符串Field Column在对Loan_number列进行排序时,它的排序不正确.
例如:
页面加载
Loan_number列显示为
9898398279
9898398277
9898398278
111
999
777
排序后的Loan_number列显示为
999
9898398277
9898398278
9898398279
111
777
排序功能如下:
Grid View having with various column between those Loan_number is string Field Column when sorting Loan_number column it is not sorted properly.
Ex:
On Page Load
Loan_number Column display as
9898398279
9898398277
9898398278
111
999
777
After Sorting Loan_number Column Appeared as
999
9898398277
9898398278
9898398279
111
777
Sorting Function as below:
protected DataTable SortDataTable(DataTable dataTable, bool isPageIndexChanging)
{
DataTable dataTable1 = new DataTable();
if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
if (GridViewSortExpression != string.Empty)
{
if (isPageIndexChanging)
{
dataView.Sort = string.Format("{0} {1}",
GridViewSortExpression, GridViewSortDirection);
}
else
{
dataView.Sort = string.Format("{0} {1}",
GridViewSortExpression, GetSortDirection());
}
}
dataView.ToTable("dataTable");
dataTable1 = CreateTable(dataView);
return dataTable1;
}
else
{
return new DataTable();
}
}
推荐答案
如果不包含字母,则将loan_number转换为数字.
从sql如下获取数据,
convert loan_number in to numeric if it will not contain alphabet ever.
fetch data from sql like below,
select convert(numeric,'999') -- replace underline with Loan_Number in your query
现在,该字段为数字,因此它将正确应用排序.
祝您编码愉快!
:)
now, field is numeric so it will apply sorting properly.
Happy Coding!
:)
2个选项:
1.在查询本身中从数据库中检索数据时对其进行排序.
与此答案类似:在gridview中排序日期 [如何使用文本数据对字母序列的GridView列进行非字母排序 [ ^ ]
2.像现在一样在前端对它进行排序:使用sortcomparer.
查看这篇文章: ASP.NET GridView ASCII和数字排序 [ ^ ]
以C#排序的数字字符串 [
2 Options:
1. Sort it while retrieving the data from database in the query itself.
Similar to this answer: sort date in gridview[^]
& this article: How to Non-alphabetically Sort GridView Columns with Text Data[^]
2. Sort it in front end as you are doing right now: Use a sortcomparer.
Look at this article: ASP.NET GridView ASCII and Numeric Sorting[^]
Numeric String Sort in C#[^]
这篇关于按字符串格式排序列未正确显示.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!