How to disable sort in DataGridView?
123,751
Solution 1
foreach (DataGridViewColumn column in dataGridView.Columns)
{
column.SortMode = DataGridViewColumnSortMode.NotSortable;
}
Solution 2
Use LINQ:
Datagridview1.Columns.Cast<DataGridViewColumn>().ToList().ForEach(f => f.SortMode = DataGridViewColumnSortMode.NotSortable);
Solution 3
If you want statically make columns not sortable. You can do this way
- Open the EditColumns window of the DataGridView control.
- Select the column you want to make not sortable on the left side pane.
- In the right side properties pane, select the Sort Mode property and select "Not Sortable" in that.
Solution 4
It's very simple:
foreach (DataGridViewColumn dgvc in dataGridView1.Columns)
{
dgvc.SortMode = DataGridViewColumnSortMode.NotSortable;
}
Solution 5
You can disable it in the ColumnAdded event:
private void dataGridView1_ColumnAdded(object sender, DataGridViewColumnEventArgs e)
{
dataGridView1.Columns[e.Column.Index].SortMode = DataGridViewColumnSortMode.NotSortable;
}
Author by
Gold
Updated on October 10, 2020Comments
-
Gold over 3 years
How can I disable sort in
DataGridView
? I need to disable the headerDataGridView
sorting. -
Nyerguds almost 9 yearsAh, that fixed it. You might want to edit that answer to get the enumeration working right, though ;)
-
Tim Hutchison almost 7 yearsWhile this may answer the question, it's always best to include a description/references wherever possible.
-
Bernhard Eriksson about 5 yearsHow does this answer differ from the one from @hunter that is five years older?
-
Dôn Kayt over 3 yearsOr calling CanUserSort = "False" (VS15)