DataGrid Sorting

Posted on 2009-04-21
Last Modified: 2013-11-07
perform integer column sorting on datagrid.
I want to sort datagrid on the basis of colun header click.
My datagrid is bind to datasource which has one column that is varchar type. But it displays integer values in the datagrid.
The problem is it is not sorting integer value and treat it like string.
for example - 1 , 2,11,3,55
after sorting 1,11,2,3,55 instead of 1,2,3,11,55
Question by:replyanshu
    LVL 53

    Expert Comment

    As for the sorting problem you mentioned, the problem is likely due to the DataTable's Column type. How do you add the DataColumn into DataTable? The DataColumn has several constructors which
    mapping to the DataTable.Columns.Add methods such as
    Add(string, Type)

    #DataColumn Constructor

    and if you add the column as DataTable.Columns.Add("column name"); then, the column is added as a stirng type column and when being sorting, the column's data will be sort as ASCII index sorting.
    So if you want to have a int or float type column being sort by number, you need to add this column and specify the column type explicitly, such as DataTable.Columns.Add("int column", typeof(int));
    DataTable.Columns.Add("float column",typeof(float));


    Author Comment

    The column type is decleared in database and we can not change the type of coloumn from string to int is there any other way around?
    LVL 53

    Expert Comment

    Did you take a look at the  the sort_command sub is below:

    Dim y As Integer
    Dim x As Integer = 0
    Dim dv3 As New DataView
    dv3 = Session("ds") 'the dataset is cached
    y = dv3.Count
    While x < y
    'convert each item in the Dataview back to type Decimal
    dv3.Item(x).Row.Item("CustBenefit") =
    x += 1
    End While
    'sort all the decimals
    dv3.Sort = e.SortExpression
    'set the datasource and re-bind it
    dgResults.DataSource = dv3

    Open in new window

    LVL 53

    Accepted Solution

    How to: Customize Sorting in the Windows Forms DataGridView

    DataGrid Customization Part-II: Custom Sorting and DataGrid Column Hiding


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
    For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now