Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET / WinForms - Clear a Sort on a DataGridView

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?

like image 756
BuddyJoe Avatar asked Sep 09 '09 20:09

BuddyJoe


People also ask

How do I stop sorting in DataGrid?

By default end users can sort columns in the grid at run time. For more information, see Sorting Columns. If you choose, however, you can disable the column sorting feature by setting the CanUserSort property to False.

What is difference between DataGridView and DataGrid control in Winforms?

The DataGrid control is limited to displaying data from an external data source. The DataGridView control, however, can display unbound data stored in the control, data from a bound data source, or bound and unbound data together.

What is DataGridView in C#?

The DataGridView control provides a customizable table for displaying data. The DataGridView class allows customization of cells, rows, columns, and borders through the use of properties such as DefaultCellStyle, ColumnHeadersDefaultCellStyle, CellBorderStyle, and GridColor.


2 Answers

I've accessed the sort on the BindingSource directly:

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

like image 82
Ken Avatar answered Sep 26 '22 01:09

Ken


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.

like image 24
Joshua Drake Avatar answered Sep 24 '22 01:09

Joshua Drake