datagridview and checkbox column

B

Bhavin Parekh

Hi
I'm building windows form using C# and framework 2.0.

I have got one gridview form with two columns (one is text, another is
checkbox column).

I'm trying to write a code so user can select only one checkbox at a time,
if he tries to select another checkbox from other row, it should prompt
error msg if already one checkbox is selected.

In short, at any time, only one row checkbox should be allowed to be
selected.

I tried to use CurrentCellDirtyStateChanged and counted check value but i
couldn't cancel changes using EndEdit or CancelEdit if selected checkbox are
more than one.

Can anyone help on this?
It would be really appreciated if someone can post dummy code.

Thanks in advance...
 
M

Mansi Shah

Hi,

You can use datagridview's cellClick event.

Search the checkbox, check it, and deselect all other cehckboxes by
looping all the items of datagridview.

Private Sub dgvKeywordSearch_CellClick(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles
dgvKeywordSearch.CellClick


Dim checkBoxCell As DataGridViewCheckBoxCell =
TryCast(dgvKeywordSearch.Rows(e.RowIndex).Cells(0),
DataGridViewCheckBoxCell)

If checkBoxCell IsNot Nothing Then

If checkBoxCell.ValueType.Equals(GetType(Boolean)) Then

Dim checked As Boolean = CType(checkBoxCell.Value, Boolean)

If checkBoxCell.Value = True Then
For i = 0 To dgvKeywordSearch.Rows.Count - 1
Dim chk As DataGridViewCheckBoxCell =
TryCast(dgvKeywordSearch.Rows(i).Cells(0), DataGridViewCheckBoxCell)
chk.Value = False
Next

End If
End If

Hope this will help you !!

Regards,
Mansi Shah
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top