Click here to Skip to main content
11,715,043 members (62,408 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET Sorting
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();
       }
   }
Posted 1-Oct-12 21:13pm
Edited 1-Oct-12 21:14pm
v2
Comments
Kenneth Haugland at 2-Oct-12 3:15am
   
You should sort by number and not by string.
ravijain03 at 2-Oct-12 3:18am
   
how it is because Loan_number is string field
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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!
Smile | :)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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#[^]
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 530
1 Richard MacCutchan 275
2 Maciej Los 259
3 ManasKumarM 155
4 Frankie-C 115
0 Sergey Alexandrovich Kryukov 775
1 CHill60 415
2 Richard MacCutchan 405
3 Maciej Los 389
4 OriginalGriff 320


Advertise | Privacy | Mobile
Web04 | 2.8.150901.1 | Last Updated 2 Oct 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100