How to disable the ability to select in a DataGridView?
I'd go with this:
private void myDataGridView_SelectionChanged(Object sender, EventArgs e)
{
dgvSomeDataGridView.ClearSelection();
}
I don't agree with the broad assertion that no DataGridView
should be unselectable. Some UIs are built for tools or touchsreens, and allowing a selection misleads the user to think that selecting will actually get them somewhere.
Setting ReadOnly = true
on the control has no impact on whether a cell or row can be selected. And there are visual and functional downsides to setting Enabled = false
.
Another option is to set the control selected colors to be exactly what the non-selected colors are, but if you happen to be manipulating the back color of the cell, then this method yields some nasty results as well.
You may set a transparent background color for the selected cells as following:
DataGridView.RowsDefaultCellStyle.SelectionBackColor = System.Drawing.Color.Transparent;
Enabled
property to false
or
this.dataGridView1.DefaultCellStyle.SelectionBackColor = this.dataGridView1.DefaultCellStyle.BackColor;
this.dataGridView1.DefaultCellStyle.SelectionForeColor = this.dataGridView1.DefaultCellStyle.ForeColor;
I fixed this by setting the Enabled
property to false
.