You cannot sort the hidden column visually by clicking on the
DataGridView
as the column is not visible. One of the following options can be used, to sort the hidden column, using the
bindingSource1
to which the
DataGridView
is bound.
Option1.
Use buttons, say
Button1
and
Button2
to sort the hidden column in ascending and descending order respectively. Then in the
Click
event of buttons set the
Sort
property of
bindingSource1
as follows
bindingSource1.Sort = "ColorCode";
bindingSource1.Sort = "ColorCode DESC";
Option2
By default the
DataGridView
sorts the column in ascending and descending order alternatively, when the header of that column is clicked. To disable this the
SortMode
property of
Column
can be set to
NotSortable
and then the
ColumnHeaderMouseClick
can be handled to sort the
ColorCode
column
dataGridView1.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable;
dataGridView1.Columns[1].SortMode = DataGridViewColumnSortMode.NotSortable;
private void dataGridView1_ColumnHeaderMouseClick(object sender,
DataGridViewCellMouseEventArgs e) {
if (e.ColumnIndex==0)
bindingSource1.Sort = "ColorCode";
else if (e.ColumnIndex==1)
bindingSource1.Sort = "ColorCode DESC";
}