开发者

Disable sorting when clicking DataGridView column header

开发者 https://www.devze.com 2023-01-20 07:27 出处:网络
I have a DataGridView column header. When I click that header, the data is resorted according to the value.

I have a DataGridView column header. When I click that header, the data is resorted according to the value.

I don't want that.

DataGrid has an AllowSort propert开发者_如何学Cy. DataGridView doesn't have that. Anything I can do?


You can disable auto sort for each and every individual cells in your DataGridView:

Disable sorting when clicking DataGridView column header


You can override OnColumnAdded function:

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


You have to set that on the columns. For example,

dataGridView1.Columns["MyColumn"].SortMode = DataGridViewColumnSortMode.NotSortable;


private void dataGridView1_ColumnAdded(object sender, DataGridViewColumnEventArgs e)
{            
    e.Column.SortMode = DataGridViewColumnSortMode.NotSortable;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号