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?
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.
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.
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.
I've accessed the sort on the BindingSource directly:
((BindingSource)_dgv.DataSource).Sort = string.Empty;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With