Live Chat Icon For mobile
Live Chat Icon

How can I get a CheckBox column in a DataGrid to react to the first click

Platform: WinForms| Category: Datagrid

When you first click into a checkbox column, the checked state of the cell does not change. One way you can make the checked state change on the first click is to handle the grid’s MouseUp event, and change the check value there.

[VB.Net}
	Private myCheckBoxCol As Integer = 9  ’my checkbox column
	Private Sub DataGrid2_MouseUp(ByVal sender As Object,  ByVal e As MouseEventArgs) Handles DataGrid2.MouseUp
		Dim hti As DataGrid.HitTestInfo = Me.dataGrid2.HitTest(e.X, e.Y)
		Try
			If hti.Type = DataGrid.HitTestType.Cell AndAlso hti.Column = myCheckBoxCol Then
				Me.dataGrid2(hti.Row, hti.Column) = Not CBool(Me.dataGrid2(hti.Row, hti.Column))
			End If
		Catch ex As Exception
			MessageBox.Show(ex.ToString())
		End Try
	End Sub ’dataGrid2_MouseUp

[C#]
	private int myCheckBoxCol = 9; //my checkbox column

	private void dataGrid2_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
	{
		DataGrid.HitTestInfo hti   = this.dataGrid2.HitTest(e.X, e.Y);
		try
		{
			if( hti.Type == DataGrid.HitTestType.Cell && 
				hti.Column == myCheckBoxCol)
			{
				this.dataGrid2[hti.Row, hti.Column] = ! (bool) this.dataGrid2[hti.Row, hti.Column];
			}
		}																				catch(Exception ex)
		{
			MessageBox.Show(ex.ToString());
		}
	}

Share with

Share on twitter
Share on facebook
Share on linkedin

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.