10

What is the proper method to tell a DataGridView to stop sorting?

I have a "screen" where I tell the grid programatically to sort by column 4 and ascending. When I switch to another area I want the same grid to come in 'default'/no sort. I'm removing all the columns and adding new ones. The sort remains on the 4th column.

I don't see a way to do this with the Sort() method. Any ideas?

BuddyJoe
  • 64,613
  • 107
  • 281
  • 451
  • Yes. But I'm even switching DataSources. I'm calling DataSource = null and Columns.Clear before the switch. – BuddyJoe Sep 09 '09 at 20:34
  • I'd like to be able to allow a refresh but save the sort order. But if the user switches what they want to look at (via a TreeView) then I'd like the sort order to go away. Is this a bug? I've tried a few things, I can't figure out a way to make it go away. – BuddyJoe Sep 09 '09 at 20:40
  • 1
    Frustrating that the enum ListSortDirection has no .None value, but SortOrder does. You use SortOrder to get the value from the DataGridView but you use ListSortDirection to set it. – BuddyJoe Sep 09 '09 at 20:42
  • The switching data sources throws my theory out. Hopefully the additional info gets you some hits. – Austin Salonen Sep 09 '09 at 21:37

7 Answers7

8

I've accessed the sort on the BindingSource directly:

((BindingSource)_dgv.DataSource).Sort = string.Empty;

Ken
  • 1,720
  • 3
  • 18
  • 31
6

From MSN Forums:

The DataGridView is bound to a DataView and not the Table directly, so you need to set:

DataTable.DefaultView.Sort = String.Empty

Effectively clearing the sorting on the table and thereby the grid that is bound to it. This appears to require a Refresh of the DataGridView, unless you are using 2005, and then you can use a separate binding manager.

Joshua Drake
  • 2,581
  • 2
  • 34
  • 53
1

Not a direct answer but there is of course no good definition for 'unsorted'. Can't you sort on a (hidden) column, like an ID?

Henk Holterman
  • 236,989
  • 28
  • 287
  • 464
  • good idea. I do have a fake column that I add to the DataTable after DataAdapter loads it called "row_number" all it is used for is pagination (if I choose to turn that on). +1. This might be my answer. – BuddyJoe Sep 09 '09 at 21:45
  • 1
    There are restrictions on sorting on 'fake' columns in combination with a DataSource, see MSDN. – Henk Holterman Sep 09 '09 at 22:19
1

in the same domain as the answer from user2268720.. not the cleanest solution, but did the job for me with an unbound DGV:

            if (dgv.SortedColumn != null) {
                DataGridViewColumn col = dgv.SortedColumn;
                col.SortMode = DataGridViewColumnSortMode.NotSortable;
                col.SortMode = DataGridViewColumnSortMode.Automatic;
            }
Damian Vogel
  • 882
  • 1
  • 10
  • 16
0

Try setting the grid's itemsource to Nothing, then set it back again to the table's DefaultView

0

I had this problem today too. I have a DataGridView that swaps out its DataSource (a DataTable) regularly. I was able to finally remove the sort that the user specified when the user clicked a column-header using the following code:

    If dgv.SortedColumn IsNot Nothing AndAlso dgv.DataSource IsNot Nothing Then
        Dim dt_Sender As DataTable = DirectCast(dgv.DataSource, DataTable)
        dt_Sender.DefaultView.Sort = dt_Sender.Columns(0).ColumnName & " ASC"
    End If

This functions because I have a hidden column in my DataGridView that's always in column-index position 0. It is an integer primary key for the underlying data that happens to be sorted on data-load.

*You may have to name your index column in the DataTable if you haven't already (e.g., dt_Sender.Columns(0).ColumnName = "colIndex").

*If you use a BindingSource object as an intermediary between your DataGridView and your DataTable (as many people seem to do), then you'll have to nest your DirectCast appropriately in the second line (i.e., Dim dt_Sender As DataTable = DirectCast(dgv.DataSource, DataTable) would change to something like: Dim dt_Sender As DataTable = DirectCast(DirectCast(dgv.DataSource, BindingSource), DataTable)).

*Additionally, if your DataTable happens to be populated by a SQL Server query that doesn't have an integer primary key (perhaps a GUID?), then you could alter its source query to include a new row number and then hide the new column in your DataGridView. E.g.:

SELECT Row_Number() OVER (ORDER BY (SELECT '')) AS RowNumber, Column1, Column2, Column3
FROM SourceTable

Hopefully this solution is helpful to someone else in the future.

Sturgus
  • 747
  • 4
  • 16
-1
/// C#
/// Author : Jeudi Prando

foreach (DataGridViewColumn column in this.DataGridView1.Columns)
{
    column.SortMode = DataGridViewColumnSortMode.NotSortable;
    column.SortMode = DataGridViewColumnSortMode.Automatic;
}