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 1025323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:54:40+00:00 2026-05-16T11:54:40+00:00

My DataGridView control currently sorts using the Sort property of the bound data, but

  • 0

My DataGridView control currently sorts using the Sort property of the bound data, but it isn’t doing what I want it to do.

I have a column called Employee that is displayed as “Firstname Lastname”.

When I sort by Employee, Amy Z_Lastname is listed before John A_Lastname, meaning I would prefer to sort by last names.

I could break the Employee string up into 3 parts, include those in the DataTable, and set the sort to “Lastname, Firstname”, then hide the Lastname and Firstname columns.

I’d rather learn how to override the default IComparer (or whatever it uses) to provide instructions on how to sort the way I want (the answer I’d prefer).

  • 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-16T11:54:40+00:00Added an answer on May 16, 2026 at 11:54 am

    Custom sorting in the DataGridView

    The DataGridView knows nothing of this
    ranking, of course. The default
    sorting scenario (automatic sorting
    for a data-bound column) of invoking
    DataGridView.Sort() via a column
    header mouse click simply delegates to
    the IBindingList implementation of
    ApplySort() in the list you’ve bound
    to the grid. Normally, this list
    would be a DataView. Using my
    ObjectListView implementation, this
    would be a view of a list of arbitrary
    business objects. Either way, you end
    up comparing the properties of the
    items in the list using the
    IComparable implementation of the
    property type.

    Details

    The PropertyComparers property exposes
    a PropertyComparersCollection, which
    is a dictionary of property name keys
    and IComparer values. You can replace
    the IComparer for a property by using
    the PropertyComparersCollection.Add()
    method or the indexer (e.g.
    view.PropertyComparers[“PropName”] =
    myComparer). To revert to the default
    IComparable sorting, use the
    PropertyComparersCollection.Remove()
    method or set the IComparer value to
    null via the indexer.

    If an IComparer is added to the
    PropertyComparers collection, it will
    be used for all subsequent sorts until
    it is removed from the collection or
    it is replaced with another IComparer.
    If the ObjectListView is already
    sorted when an IComparer is added to
    or removed from PropertyComparers, the
    view will automatically be re-sorted.

    If you want to change multiple
    property comparers after the view is
    sorted, you can use the ObjectListView
    BeginUpdate() and EndUpdate() methods
    to suppress the ListChanged and Sort
    events until all of the IComparers
    have been changed. This prevents
    multiple refreshes of the
    DataGridView. If the ObjectListView
    is not sorted at the time IComparers
    are added or removed, no automatic
    re-sorting is done.

    Note that when sorting on multiple
    columns, a custom IComparer can be
    used with one sort property, and the
    default IComparable sort on another.

    e.g:

    private void radioButtonSortProgram_CheckedChanged(object sender, EventArgs e)
    
    {
    
        if (this.radioButtonSortProgramAlpha.Checked)
    
            this.view.PropertyComparers["Program"] = new CustomerProgramComparerAlpha();
    
        else if (this.radioButtonSortProgramRank.Checked)
    
            this.view.PropertyComparers["Program"] = new CustomerProgramComparerRank();
    
        else
    
            this.view.PropertyComparers.Remove("Program");
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WinForms application with a DataGridView control and a column of DataGridViewButtonCell
In my application I have a DataGridView control that displays data for the selected
I have a custom DataGridView column that uses an embedded control that pops up
I am using a DataGridView control in a Windows Forms application. When a user
I have a DataGridView control in a winforms app that I'm working on. The
I'd like to use the DataGridView control as a list with columns. Sort of
I have a calendar control derived from DataGridView. A custom item hover event is
I am trying to add datagridview control in my custom control but i failed.
hi i have datagridview name data1, and binding data to data1 from database, when
I have one DataGridView and want to make the Header text Bold. I have

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.