How to disable sort in DataGridView?

C#.NetWinformsSortingDatagridview

C# Problem Overview


How can I disable sort in DataGridView? I need to disable the header DataGridView sorting.

C# Solutions


Solution 1 - C#

foreach (DataGridViewColumn column in dataGridView.Columns)
{
    column.SortMode = DataGridViewColumnSortMode.NotSortable;
}

Solution 2 - C#

Use LINQ:

Datagridview1.Columns.Cast<DataGridViewColumn>().ToList().ForEach(f => f.SortMode = DataGridViewColumnSortMode.NotSortable);

Solution 3 - C#

If you want statically make columns not sortable. You can do this way

  1. Open the EditColumns window of the DataGridView control.
  2. Select the column you want to make not sortable on the left side pane.
  3. In the right side properties pane, select the Sort Mode property and select "Not Sortable" in that.

Solution 4 - C#

It's very simple:

foreach (DataGridViewColumn dgvc in dataGridView1.Columns)
{
    dgvc.SortMode = DataGridViewColumnSortMode.NotSortable;
}

Solution 5 - C#

You can disable it in the ColumnAdded event:

private void dataGridView1_ColumnAdded(object sender, DataGridViewColumnEventArgs e)
{
    dataGridView1.Columns[e.Column.Index].SortMode = DataGridViewColumnSortMode.NotSortable;
}

Solution 6 - C#

private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
      for (int i = 0; i < dataGridView1.Columns.Count; i++)
      {
           dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
      }
}

Solution 7 - C#

If you can extend the DataGridView you can override the Sort method with am empty one. This disables Sort for the DataGridView entirely.

public override void Sort(DataGridViewColumn dataGridViewColumn, ListSortDirection direction)
{
    //base.Sort(dataGridViewColumn, direction);
}

Note: You cannot even programmatically sort any column.

Solution 8 - C#

For extending control functionality like this, I like to use extension methods so that it can be reused easily. Here is a starter extensions file that contains an extension to disable sorting on a datagridview.

To use it, just include it in your project and call like this

myDatagridView.DisableSorting()

In my case, I added this line of code in the DataBindingComplete eventhandler of the DataGridView where I wanted sorting disabled

Imports System.ComponentModel
Imports System.Reflection
Imports System.Runtime.CompilerServices
Imports System.Windows.Forms

Public Module Extensions

<Extension()>
Public Sub DisableSorting(datagrid As DataGridView)
    For index = 0 To datagrid.Columns.Count - 1
        datagrid.Columns(index).SortMode = DataGridViewColumnSortMode.NotSortable
    Next
End Sub


End Module

Solution 9 - C#

I was looking for a way to disable my already existing DataGridView and came across several answers. Oddly enough, the first few results on google were some very old topics. This being the earliest one of them, I decide to put my answer here.

private void dgvDetails_ColumnStateChanged(object sender, DataGridViewColumnStateChangedEventArgs e)
{
	e.Column.SortMode = DataGridViewColumnSortMode.NotSortable;
}

The description when you click on ColumStateChanged in the properties window is:

> "Occurs when a column changes state, such as gaining or loosing focus"

Granted there are many ways to do this but I thought I'd add this one here. Can't say I found it anywhere else but then again I only read the first 5 topics I found.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGoldView Question on Stackoverflow
Solution 1 - C#hunterView Answer on Stackoverflow
Solution 2 - C#Amit BishtView Answer on Stackoverflow
Solution 3 - C#Banusundar ArumugamView Answer on Stackoverflow
Solution 4 - C#Dara.JoukarView Answer on Stackoverflow
Solution 5 - C#DeepakView Answer on Stackoverflow
Solution 6 - C#Fatih GurbuzView Answer on Stackoverflow
Solution 7 - C#bansiView Answer on Stackoverflow
Solution 8 - C#Scope CreepView Answer on Stackoverflow
Solution 9 - C#user1242840View Answer on Stackoverflow