Click here to Skip to main content
12,944,738 members (50,719 online)
Rate this:
 
Please Sign up or sign in to vote.
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:
 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
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
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!
:)
  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
Top Experts
Last 24hrsThis month
OriginalGriff 4,651
CHill60 3,055
Maciej Los 2,423
Jochen Arndt 1,910
ppolymorphe 1,765


Advertise | Privacy | Mobile
Web02 | 2.8.170518.1 | Last Updated 2 Oct 2012
Copyright © CodeProject, 1999-2017
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