Best way to disable the column header sorting in DataGridView?

No, I think setting the SortMode on the column directly is as good as it gets. But honestly, who cares? What's so bad about a simple loop?

According to MSDN Modifying Sorting Behavior for Individual Columns Setting the AllowSorting property of the control enables sorting for columns by default. You can disable sorting for individual fields (for example, the BoundField or TemplateField field) by setting the SortExpression property of the individual column to an empty string (""). Hope it will help you.

If it bothers you to loop over the columns or you have multiple DataGridView's, you can write an extension method for this: public static class DatatGridViewExtensions { public static void SetColumnSortMode(this DataGridView dataGridView, DataGridViewColumnSortMode sortMode) { foreach (var column in dataGridView. Columns) { column. SortMode = sortMode; } } } } Use it like this: BalancesGridView.

SetColumnSortMode(DataGridViewColumnSortMode. NotSortable).

This doesn't really achieve anything. This makes all columns in the datagridview not sortable. The extension method itself isn't a bad idea, but it's incomplete.It needs to have another parameter and maybe even an overload to allow a column id, column name or a collection of either to be passed in.

– Joel Etherton Dec 2 '10 at 12:44 @Joel Etherton: Read his question again. The OP want's to disable column header sorting in general. – VVS Dec 2 '10 at 13:33.

You can cancel the event. Protected void gridViewSortingEvent(object sender, GridViewSortEventArgs e) { e. Cancel = true; }.

I did not see a 'GridViewSortingEvent' in the list of events that is available for DataGridView. There seems to be a sorting event for GridView, but not DataGridView. The closest I could find to implement this is to cancel the DataGridView.

ColumnHeaderMouseClick event. – Samik R Jul 29 at 14:17.

I ran into this problem today. I wrote this method and called it on the form load event. Public void DisableGridviewSorting(DataGridView grid, int index) { //Index = DataGridView.Columns.

Count for (int I = 0; I Then you can set non-sortable for each individual column.

If it bothers you to loop over the columns or you have multiple DataGridView's, you can write an extension method for this.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions