DataGridView disable sorting

I needed to disable sorting in a DataGridView. I thought I would find something like DataGridView.AllowSort and simply set to AllowSort=false, but I couldn’t find it.

Finally, I figured it out. You need to access each column in the DataGridView and set it as NotSortable.

This is a small code snipet to disable the sorting of DataGridView.

 

for (int i = 0; i < dataGridView1.ColumnCount; i++)

{

  dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;

}

I hope that helps.