C # DataGridView Disable sort icon from column header

I have a Winforms DataGridView with several columns that can be sorted, and some that could not be. For columns that I don't want gridview sort to be sorted, I set

dgvConnections.Columns[e.ColumnIndex].SortMode = DataGridViewColumnSortMode.NotSortable; in

dgvConnections_ColumnHeaderMouseClick event handler, but I canโ€™t get rid of the sort icon from the column header, as its presence can cause confusion for the user.

So kindly offer me a way to get rid of the sort icon from a column that cannot be sorted. Thanks in advance!

+5
source share
1 answer

It seems that you do not support the correct execution order. The code you wrote will do the job

 dgvConnections.Columns[e.ColumnIndex].SortMode = DataGridViewColumnSortMode.NotSortable; 

but make sure you call it after initializing the DataGridView.

If you want to use it for specific columns, as you mentioned in your question, you need to find the index and set sortmode.

+3
source

All Articles