Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
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();
       }
   }
Posted
Updated 1-Oct-12 21:14pm
v2
Comments
Kenneth Haugland 2-Oct-12 3:15am    
You should sort by number and not by string.
ravijain03 2-Oct-12 3:18am    
how it is because Loan_number is string field

convert loan_number in to numeric if it will not contain alphabet ever.

fetch data from sql like below,
SQL
select convert(numeric,'999') -- replace underline with Loan_Number in your query 


now, field is numeric so it will apply sorting properly.

Happy Coding!
:)
 
Share this answer
 
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#[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900