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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:10:47+00:00 2026-05-26T18:10:47+00:00

I have a databound DataGridView in a Win Forms app which the user may

  • 0

I have a databound DataGridView in a Win Forms app which the user may have sorted by a column. The problem is this: after the user leaves a row after editing a cell in the sorted column, the row is immediately re-sorted.

This is very disorienting for users and makes editing groups of rows together impossible.

The solution I’m looking for will effectively disable automatic re-sorting after an initial sort and then only sort again when the user requests it.

  • 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-26T18:10:48+00:00Added an answer on May 26, 2026 at 6:10 pm

    For the benefit of others, here is the solution I came up with, but I’d love to hear a better one.

    I added an additional, non-persistent column to the DataTable called SORT_ORDER which is used only for sorting.

    When the user clicks a column to sort, I copy the values and value type from the selected column to the SORT_ORDER column and then sort on SORT_ORDER. Since the SORT_ORDER is not visible and can’t be edited, the sort order does not change even if the user edits the selected column. The event handler looks like this:

        private void MyDataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
            dirtyCellListenerEnabled = false;
    
            SORT_ORDER.ValueType = MyDataGridView.Columns[e.ColumnIndex].ValueType;
    
            foreach(DataGridViewRow r in MyDataGridView.Rows) {
                r.Cells[SORT_ORDER.Index].Value = r.Cells[e.ColumnIndex].Value;
            }
    
            switch(MyDataGridView.SortOrder) {
                case System.Windows.Forms.SortOrder.None:
                    MyDataGridView.Sort(SORT_ORDER, ListSortDirection.Ascending);
                    break;
                case System.Windows.Forms.SortOrder.Ascending:
                    MyDataGridView.Sort(SORT_ORDER, ListSortDirection.Descending);
                    break;
                case System.Windows.Forms.SortOrder.Descending:
                    MyDataGridView.Sort(SORT_ORDER, ListSortDirection.Ascending);
                    break;
            }
            dirtyCellListenerEnabled = true;
        }
    

    Note that I had to disable and re-enable my cell listener so that my code doesn’t treat the sort column update as a real change.

    Before arriving at this solution, I had also tried adding the sort column to the DataGridView, but it doesn’t work because the DataGridView can’t sort on a column that doesn’t exist in its data source.

    I’m sure there are some other tweaks I could do, too, like suspending updates while populating the SORT_ORDER and displaying the sort glyph on the selected column.

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

Sidebar

Related Questions

I have a DataGridView with a DataGridViewCheckBoxColumn column, which is databound to a list.
I have a Winforms app that has a DataGridView that is databound at runtime.
I have a DataGridView in a C# WinForms app that is DataBound at runtime
I have a page which is databound to an object like so this.DataContext =
I have a DataBound GridView. However I have one column where the value comes
I have a databound list view and can set all the column widths manually
I have a databound TextBlock control (which is being used inside a DataTemplate to
I have a databound listbox which is actually displaying two columns of data. It
I have a TreeView databound to a CollectionViewSource Groups collection. This is so that
I have a combobox databound to the available system colors. When the user selects

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.