0
votes

Are there any method to disable the RowHeader and ColumnHeader from being clicked or selected or mouseClick or mouseHover. Im looking for:

dgv.RowHeader.Enable = false;//disable rowheader

or

dgv.RowHeaderClick += dgv_RowHeaderClick;
private void dgv_RowHeaderClick(object sender, EventArgs e)
    {
        DataGridView dgv = (sender as DataGridView);

        dgv.RowHeaderClick -= dgv_RowHeaderClick;
    }

This is an annoying property of DataGridView when a user click on headers, it will highlight and turn blue. Also exception throw sometimes when clicking the cells of headers.

1

1 Answers

1
votes

If you don't mind disabling the automatic sort mode of the DataGridView column Headers, you could disable the Column.SortMode, setting it to DataGridViewColumnSortMode.NotSortable.
This will prevent the colum Header from being highlighted and no sorting glyph is shown.

A Mouse click won't produce any notable effect; the CellClick event is raised (with e.RowIndex = -1) as usual.

foreach (DataGridViewColumn col in dataGridView1.Columns) {
    col.SortMode = DataGridViewColumnSortMode.NotSortable;
}

If the DataGridView is bound to a DataSource and it's set to AutoGenerateColumns, repeat the procedure if the DataSource is changed. You could also set this property in the DataGridView designer interface, if the Columns are pre-defined.

For the RowHeader, if needed, you could change the DataGridView.SelectionMode to DataGridViewSelectionMode.CellSelect.