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

The Archive Base Latest Questions

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

I believe this should be handled automatically. I am binding a DataGridView to an

  • 0

I believe this should be handled automatically. I am binding a DataGridView to an array of objects:

public class Entity {    
    public string Name { get; set; }     
    public int PrimaryKey { get; set; }
}

Binding the grid:

public void BindGrid(Entity[] entities) {
    grdEntities.DataSource = entities;
}

When I click the column header in the “Name” column, nothing is happening, even though the SortMode is set to Automatic. The sort glyph is also missing from the column header.

I have tried binding to an IBindingList and also an IList but this did not work.

I am hoping there is a simple, elegant solution with setting properties on either the DataGridView or the DataGridViewColumn rather than having to make a new class to support sorting. What should I be doing to support sorting on a column by clicking the header on a DataBound DataGridView?

  • 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-23T03:08:56+00:00Added an answer on May 23, 2026 at 3:08 am

    I created a new IComparer based interface that allows you to specify both a column and a direction. I only did this because I need my sorting code to be as generic as possible – I have TWO grids that need to sort like this, and I don’t want to maintain twice the code. Here is the interface, quite simple:

       public interface IByColumnComparer : IComparer
       {
          string SortColumn { get; set; }
          bool SortDescending { get; set; }
       }
    

    Obviously, if you’re not worried about keeping things generic (you probably should) than this isn’t strictly necessary. Then, I built a new class that is based on BindingList<>. This allowed me to override the sorting code and provide my own IByColumnComparer on a column by column basis which is what allowed for the flexibility I needed. Check this out:

    public class SortableGenericCollection<T> : BindingList<T>
    {
      IByColumnComparer GenericComparer = null; 
    
      public SortableGenericCollection(IByColumnComparer SortingComparer)
      {
         GenericComparer = SortingComparer;
      }
    
    
      protected override bool SupportsSortingCore
      {
         get
         {
            return true;
         }
      }
    
      protected override bool IsSortedCore
      {
         get
         {
            for (int i = 0; i < Items.Count - 1; ++i)
            {
               T lhs = Items[i];
               T rhs = Items[i + 1];
               PropertyDescriptor property = SortPropertyCore;
               if (property != null)
               {
                  object lhsValue = lhs == null ? null :
                  property.GetValue(lhs);
                  object rhsValue = rhs == null ? null :
                  property.GetValue(rhs);
                  int result;
                  if (lhsValue == null)
                  {
                     result = -1;
                  }
                  else if (rhsValue == null)
                  {
                     result = 1;
                  }
                  else
                  {
                     result = GenericComparer.Compare(lhs, rhs); 
                  }
                  if (result >= 0)
                  {
                     return false;
                  }
               }
            }
            return true;
         }
      }
    
      private ListSortDirection sortDirection;
      protected override ListSortDirection SortDirectionCore
      {
         get
         {
            return sortDirection;
         }
      }
    
      private PropertyDescriptor sortProperty;
      protected override PropertyDescriptor SortPropertyCore
      {
         get
         {
            return sortProperty;
         }
      }
    
      protected override void ApplySortCore(PropertyDescriptor prop,
      ListSortDirection direction)
      {
         sortProperty = prop;
         sortDirection = direction;
    
         GenericComparer.SortColumn = prop.Name;
         GenericComparer.SortDescending = direction == ListSortDirection.Descending ? true : false;
    
         List<T> list = (List<T>)Items;
         list.Sort(delegate(T lhs, T rhs)
         {
            if (sortProperty != null)
            {
               object lhsValue = lhs == null ? null :
               sortProperty.GetValue(lhs);
               object rhsValue = rhs == null ? null :
               sortProperty.GetValue(rhs);
               int result;
               if (lhsValue == null)
               {
                  result = -1;
               }
               else if (rhsValue == null)
               {
                  result = 1;
               }
               else
               {
                  result = GenericComparer.Compare(lhs, rhs);
               }
               return result;
            }
            else
            {
               return 0;
            }
         });
      }
    
      protected override void RemoveSortCore()
      {
         sortDirection = ListSortDirection.Ascending;
         sortProperty = null;
      }
    }
    

    EDIT This should provide some information about how to create your own IComparer based on my interface above. The advantage to having your own IComparer based on the interface is that you can sort some columns one way, and other columns another (some columns might be strings, and some ints, some might have special rules about what goes on top, etc). Here is an example of how your IComparer might work:

    public class MyGenericComparer : IByColumnComparer
    {
      private string columnToCompare;
      private bool descending;
    
      public string SortColumn
      {
         get { return columnToCompare; }
         set { columnToCompare = value; }
      }
    
      public bool SortDescending
      {
         get { return descending; }
         set { descending = value; }
      }
    
      public MyGenericComparer(string column, bool descend)
      {
         columnToCompare = column;
         descending = descend;
      }
    
      public int Compare(object x, object y)
      {
         MyGenericObject firstObj = (MyGenericObject )x;
         MyGenericObject secondObj = (MyGenericObject )y;
    
         if (descending) 
         {
            MyGenericObject tmp = secondObj ;
            secondObj = firstObj ;
            firstObj = tmp;
         }
    
         if (columnToCompare == "StringColumn")
         {
            //Run code to compare strings, return the appropriate int
            //eg, "1" if firstObj was greater, "-1" is secondObj, "0" if equal
         }
    
         if (columnToCompare == "IntColumn")
         {
            //Run code to compare ints, return the appropriate int
            //eg, "1" if firstObj was greater, "-1" is secondObj, "0" if equal
         }
      }
    }
    

    Then all you have to do is create your list with an instance of your comparer!

    public static MyGenericComparer GridComparer = new MyGenericComparer();
    public static SortableGenericCollection<GenericObject> GridList = new SortableGenericCollection<GenericObject>(GridComparer);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Should links on a web page ALWAYS be underlined? I do not believe this
I have been told and I'm not sure I believe this: Removing white space
EDIT: Modified title and added update. UPDATE : We no longer believe this is
I believe there is a way to do this, but I'm not familiar with
I had this answer on another post I asked: I believe the VS designer
(I've tried this in MySql) I believe they're semantically equivalent. Why not identify this
ADDED: This question is now, I believe, subsumed by this one: Using GNU Screen
Although this question and this question are close to what I'm asking, I believe
I found this solution which works, but I find it hard to believe there
There gotta be an easy way to do this, I can't believe there's none.

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.