How to disable sort in DataGridView?

前端 未结 9 662
孤城傲影
孤城傲影 2020-12-04 23:35

How can I disable sort in DataGridView? I need to disable the header DataGridView sorting.

相关标签:
9条回答
  • 2020-12-04 23:57

    If you can extend the DataGridView you can override the Sort method with am empty one. This disables Sort for the DataGridView entirely.

    public override void Sort(DataGridViewColumn dataGridViewColumn, ListSortDirection direction)
    {
        //base.Sort(dataGridViewColumn, direction);
    }
    

    Note: You cannot even programmatically sort any column.

    0 讨论(0)
  • 2020-12-05 00:00

    If you want statically make columns not sortable. You can do this way

    1. Open the EditColumns window of the DataGridView control.
    2. Select the column you want to make not sortable on the left side pane.
    3. In the right side properties pane, select the Sort Mode property and select "Not Sortable" in that.
    0 讨论(0)
  • 2020-12-05 00:06

    I was looking for a way to disable my already existing DataGridView and came across several answers. Oddly enough, the first few results on google were some very old topics. This being the earliest one of them, I decide to put my answer here.

    private void dgvDetails_ColumnStateChanged(object sender, DataGridViewColumnStateChangedEventArgs e)
    {
        e.Column.SortMode = DataGridViewColumnSortMode.NotSortable;
    }
    

    The description when you click on ColumStateChanged in the properties window is:

    "Occurs when a column changes state, such as gaining or loosing focus"

    Granted there are many ways to do this but I thought I'd add this one here. Can't say I found it anywhere else but then again I only read the first 5 topics I found.

    0 讨论(0)
提交回复
热议问题