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

  • Home
  • SEARCH
  • 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 9095707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:40:24+00:00 2026-06-16T23:40:24+00:00

Setup I have a piece of code that is sorting a DataGridView using a

  • 0

Setup

I have a piece of code that is sorting a DataGridView using a custom IComparer:

public class CustomComparer: IComparer
{
    public int Compare(object x, object y)
    {
        DataGridViewRow row1 = (DataGridViewRow)x;
        DataGridViewRow row2 = (DataGridViewRow)y;

        if (row1.ReadOnly && row2.ReadOnly)
        {
            return 0;
        }
        else if (row1.ReadOnly && !row2.ReadOnly)
        {
            return 1;
        }
        else
        {
            return -1;
        }
}

Problem

Strangely, when I execute the following line (after populating the rows):

grid.Sort(new CustomComparer());

I get an ArgumentOutOfRangeException with the message “Index was out of range. Parameter: index”.

More Facts

Further investigation revealed the following:

  • The DataGridView I’m sorting doesn’t have a BindingSource on it – rows have been manually added.
  • The Stack Trace of the error is only one level deep – it occurs on an InternalDictionary in mscorlib
  • Strange fact #1 – This only happens if at any point, my custom comparer returns -1 for any of its comparisons
  • If I change the Sort method to no longer use my CustomComparer, the exception is not thrown.

Workaround

This last fact led me to rewrite the Compare() method to defer to .NET’s CompareTo method:

DataGridViewRow row1 = (DataGridViewRow)x;
DataGridViewRow row2 = (DataGridViewRow)y;

return row1.ReadOnly.CompareTo(row2.ReadOnly);

Which mysteriously worked. The exception is no longer thrown.

So although I have a workaround, I wonder if anyone has any idea why this could possibly be a fix, and what the issue might have been in the first place. I’ve looked at the implementation of CompareTo and it also returns -1…

  • 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-06-16T23:40:25+00:00Added an answer on June 16, 2026 at 11:40 pm

    juharr is right, but here’s why he’s right:

    Your implementation of Compare is not symmetric, meaning if row1.ReadOnly == false and row2.ReadOnly == false you return -1, meaning “row1 is less than row2“. If you turn that comparison around with the same values, then row2 becomes less than row1. This is likely confusing the sort algorithm that requires Compare to be symmetric.

    The correct comparison should be:

        if (row1.ReadOnly == row2.ReadOnly)  // change && to ==
        {
            return 0;
        }
        else if (row1.ReadOnly && !row2.ReadOnly)
        {
            return 1;
        }
        else
        {
            return -1;
        }
    

    which is likely what bool.CompareTo(bool) would return, which is why your “workaround” (which is a better solution, in my opinion) works.

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

Sidebar

Related Questions

I have a piece of code that setup a ListModel based on content of
I have the following piece of code: // setup the AJAX request var pageRequest
I have a piece of code that needs to run every day at a
I have a piece of C++ code that generates the data. I want to
I have a form that sends a update sql piece of code to my
I have an piece of code that does calculations on assets. There are many
I have a piece of code that shows me a Satellite version of Google
I have a piece of code that as far as I can tell should
I have the following piece of code that I am having problems resetting the
Setup Have you ever had the experience of going into a piece of code

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.