Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 537401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:55:23+00:00 2026-05-13T09:55:23+00:00

I am using just a simple DataGridView to hold a bunch of data (Funny

  • 0

I am using just a simple DataGridView to hold a bunch of data (Funny that).

I have decimals in a particular column. But when it comes to ordering by that decimal column, it orders it incorrectly. For example :

Starting order might be :

  • 0.56
  • 3.45
  • 500.89
  • 20078.90
  • 1.56
  • 100.29
  • 2.39

The ending order would be :

  • 0.56
  • 100.29
  • 1.56
  • 20078.90
  • 2.39
  • 3.45
  • 500.89

As you can see, it orders it starting from the first number. And then orders it in this way.

I thought possibly I could set the column to a different “ColumnType” and that may automatically do it. But there is no “Numeric” or “Decimal” column types.

I was on MSDN looking up the issue, and I could find the “sort” method that I can use on the DataGridView. But the explanation was a bit over my head, and the examples didn’t use numbers, only text so I couldnt see how I was supposed to switch things up.

Any help would be much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:55:24+00:00Added an answer on May 13, 2026 at 9:55 am

    You can solve this by adding a handler for the SortCompare event on the DataGridView with the following code:

    private void dataGridView1_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
    {
        if (e.Column.Index == 0)
        {
            if (double.Parse(e.CellValue1.ToString()) > double.Parse(e.CellValue2.ToString()))
            {
                e.SortResult = 1;
            }
            else if (double.Parse(e.CellValue1.ToString()) < double.Parse(e.CellValue2.ToString()))
            {
                e.SortResult = -1;
            }             
            else
            {
                e.SortResult = 0;
            }
            e.Handled = true;
       }
    }
    

    From MSDN there is this description of the SortResult values:

    Less than zero if the first cell will
    be sorted before the second cell; zero
    if the first cell and second cell have
    equivalent values; greater than zero
    if the second cell will be sorted
    before the first cell.

    Note that in my test bed the only numeric column was the first (with index 0) so that is why I have the check on the column index.

    Also, depending on your needs and data you may want to refine my code – for example, my code will throw an exception if for some reason you have non numeric data in your column.

    You have probably seen it, but here is a link to the MSDN page on customising the DataGridView sorting. As you say, they only deal with text.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm an IIS guy and know its as simple as just using the http://[computername]/path
Is it a simple case of just never using the this.XYZ construct?
I wrote a simple web service in C# using SharpDevelop (which I just got
I need to query the table for all but keyword. Using just - doesn't
I know that just using rand() is predictable, if you know what you're doing,
Have just started using Google Chrome , and noticed in parts of our site,
How would I implement a binary search using just an array?
I am using Carbide (just upgraded to 2.0) to develop an S60 3rd Edition
Is there an ASP.NET AJAX framework other than just using UpdatePanel & Friends or
What would be the benefit of using decimal.compare vs. just using a > or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.