Tag Archives: datagridview

Datagridview checkbox can’t be checked (Forms)

If a DataGridView with a checkbox column can’t be checked verify whether editing has been
disabled. EditMode set to EditOnKeystrokeOrF2 appears to work OK.

To prevent editing of specific columns, use the CellBeginEdit event or set the ReadOnly property of the column.

private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
}

Also make sure the DataGridView is Enabled and not ReadOnly!

Lastest update in February 2021, inital post in September 2017

DataGridView: detect click on empty cells (Forms)

To detect a mouse click on empty cells add the mouseClick event.

private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
            DataGridView.HitTestInfo ht = dataGridView1.HitTest(e.X, e.Y);
            if (ht != null)
            {
		  //e.RowIndex and e.ColumnIndex can be used.
	     }
}       
Lastest update in September 2017, inital post in September 2017

DataGridView: No selection mode (Forms)

To prevent selection in a DataGridView add the SelectionChanged event

private void dataGridView_SelectionChanged(object sender, EventArgs e)
{
	//Just to be sure this will not become recursive 
	if (dataGridView.SelectedRows.Count > 0) dataGridView.ClearSelection();  
}
Lastest update in February 2021, inital post in September 2017

Remove all selected rows from an editable DataGridView

Remove all selected rows from an editable DataGridView.

There is small catch here. The new row is also selected but will trigger an exception when removed.

The following code will work correctly.

foreach (DataGridViewRow dr in list.SelectedRows)
{
   if (!dr.IsNewRow)
   {
      list.Rows.Remove(dr);
   }
}
Lastest update in May 2011, inital post in May 2011