Disable Column Sort in DataGridView

DataGridView has column sorting enabled by default. It’s usually a good thing but in some rare cases it can be a problem and should be disabled. To disable it, add a ColumnAdded event:
this.dataGridView1.ColumnAdded += new System.Windows.Forms.DataGridViewColumnEventHandler( this.dataGridView1_ColumnAdded);
and add a function to disable the sorter on the column which is being added like this:
private void dataGridView1_ColumnAdded(object sender, DataGridViewColumnEventArgs e)
{
dataGridView1.Columns[e.Column.DisplayIndex].SortMode = DataGridViewColumnSortMode.NotSortable;
}

This entry was posted in C#, Programming, Tutorials. Bookmark the permalink.

3 Responses to Disable Column Sort in DataGridView

  1. Emyu says:

    Good one. better than looping through columns – which in any case won’t work if grid columns are added and removed dynamically

  2. Saqib says:

    Thanks a lot.
    Was looking all over for this.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>