Buena
Buena

Reputation: 2491

How to make DataGridView NotSortable

DGV has no AllowSort property.
How can I prevent user to sort the items?

Upvotes: 1

Views: 1287

Answers (3)

BriOnH
BriOnH

Reputation: 945

Set the sort value programmatically:

this.dataGridView1.Columns["Priority"].SortMode = DataGridViewColumnSortMode.NotSortable;

or for the whole DataGrid (I am on my mac and dont have my VM MS Machine up so something like):

foreach(dataGridView.Columns x in this.dataGridView1.Columns)
{
    x.SortMode = DataGridViewColumnSortMode.NotSortable;
}

http://msdn.microsoft.com/en-us/library/8b9k0ktw.aspx

Upvotes: 1

ChristopheD
ChristopheD

Reputation: 116127

This thread describes some possible solutions:

  1. Use the ColumnAdded event:

    private void MyDGV_ColumnAdded(object sender, DataGridViewColumnEventArgs e)
    {
        e.Column.SortMode = DataGridViewColumnSortMode.NotSortable;
     }
    
  2. Override OnColumnAdded:

    protected override void OnColumnAdded(DataGridViewColumnEventArgs e)
    {
        base.OnColumnAdded(e);
        e.Column.SortMode = DataGridViewColumnSortMode.NotSortable;
    }
    

Upvotes: 0

Angshuman Agarwal
Angshuman Agarwal

Reputation: 4866

Loop over gridview columns and set the SortMode of each column as this -

DataGridViewColumnSortMode.NotSortable

You can also do this via Designer :: Go to Columns collection > Set SortMode as NotSortable

Upvotes: 0

Related Questions