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.

Comments (1)

  1. Imran Khan says:

    Thanks a lot.

    i have implement this code in my application and working perfect

    private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)

           {

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

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

           }