views:

256

answers:

1

We have a DataGridViewCheckBox column bound to a boolean property in our class.

The property setter has some logic which says that under certain conditions a True flag cannot be changed, ie, it stays checked forever. This is on a per record basis. So the entire column can't be readonly, only certain rows.

Pseudo code:

Public Property Foo() As Boolean
    Get
       Return _Foo
    End Get
    Set(ByVal value As Boolean)
        If _Foo And Bar And value = False Then
            //do nothing, in this scenario once you're true, you stay true
        Else
            _Foo = value
        End If
    End Set
End Property

Databinding is handling all of this fine, except that the checkbox is visibly cleared when it's clicked. Then, of course, when the binding / setter is fired (as you move off that cell) it is restored to its checked status per the underlying logic. Ultimately it doesn't matter too much but it's a clumsy UI.

How can we intercept the user's click and keep it checked?

A: 

This does the job but are there more elegant solutions?

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

    //allow checking, but don't allow unchecking
    If Grid1.Columns("myColumn").Index = e.ColumnIndex Then
        Dim chkbox As DataGridViewCheckBoxCell = Grid1.CurrentCell
        chkbox.ReadOnly = True
        chkbox.Value = True
    End If

End Sub
hawbsl