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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:45:20+00:00 2026-05-13T12:45:20+00:00

This is a C# Winform question. I have a DataGridView which is bounded to

  • 0

This is a C# Winform question. I have a DataGridView which is bounded to a DataTable. I construct the DataTable myself, which several DataColumn instances. When the DataTable is bound to the DataGridView, by default, every column is sortable by clicking the headers of the DataGridView.

But the sorting behavior is something “by default”. It seems that it is sorted by string. This is true even if I put this as my code:

DataColumn dc = new DataColumn(“MyObjectColumn”, typeof(MyObject));

And MyObject has overriden ToString() and has implemented the IComparable interface. That means even if I have told the DataTable how to sort the special column with the implementation of IComparable interface, DataGridView still doesn’t do it the way I expect.

So how can I let DataTable sort data in the way I want?

Thanks for the answers.

  • 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-13T12:45:20+00:00Added an answer on May 13, 2026 at 12:45 pm

    I would recommend using the DefaultView of the DataTable. It has some built in sorting features that are little more extendable. The easiest is RowFilter, but I’m not sure if this will be what you’re looking for if your data types are overridden as .ToString() at the table level.

    EDIT: added code snippet

    A custom method like this that maybe even overrides or is called during the sort event of your DataGridView might be able to sort a DataView before the binding actually occurs. But as I understand it, the IComparable never gets called unless you specify it to be called.

    protected void SortGrid()
    {
        System.Data.DataView dv = myDataTable.DefaultView;
    
        myOjbect comparer = new MyObject();
    
       // Comparer specifics go here. Sort order, column/fieldname etc
       // or any custom properties used in sorting
    
       dv.Sort(comparer)
    
       dgMyGrid.DataSource = dv
       dgMyGrid.DataBind()
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is more of a design question. The requirement is to have a winform
i have a winform app that contains a form1 and the program.cs file. in
I've been writing some custom WinForm controls which do a pretty heavy amount of
I am learning boo (got the ebook), but one question I have is what
I have been using WPF and Xaml for a while and like it quite
i created a winforms application. what it does is it queries a database and
I am using .Net (C#, WinForms, Linq to SQL, SQL Server 2008) to develop
I'd like to open a new email page, in Lotus Notes (I know I
i am developing a notification service in c# for our web site. its windows
It's a pretty standard setup, WinForms client > Self-serving Business/Data layer > SQL Server

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.