datagridview handle event options

jamie123

Well-known member
Joined
May 30, 2008
Messages
82
Programming Experience
Beginner
I have a datagridview with 2 checkboxes. These checkboxes are independent of each other and do different things. One is under the column "Insurance" , the other is under the column "Tax" . When I check "Insurance" it brings up a form. When I uncheck "Insurance" It brings up a message box. Tax does not do anything other than become check or checked.

Now, I have code that semi-works, but I am handling the datagridview.cellcontentclick event, and I also have a variable declared as the boolean value for the insurance text box,the boolean values are flipped (by that i mean, when i check the checkbox the value is false, because it gets the value before it sets the check, and when i uncheck the checkbox the value is true) let's say both the tax and the insurance check box values are false(neither of them are checked), i click the insurance check box, it goes through the procedure:

(pseudocode)
VB.NET:
If insurancecheckbox = false then
   'do this code'
End If

that works fine. But, if i check the Tax checkbox, it will still do the same code as it did if ic hecked the insurance checkbox. I can't have that, so i'm wondering if there is anyway I could handle a specific column click within the datagridview? That would make things a hell of a lot easier, if not though, what can I do to my code so that I can specify and differentiate between when Tax is checked/unchecked and Insurance is checked/unchecked. And as I said before, Tax does not do anything, Insurance is the only checkbox that requires code. Here is my code as of now, it confuses the tax and the insurance checkboxes

VB.NET:
   Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        'checks if checkbox is checked, if it is, brings up frmIns, 
        'if you're unchecking the checkbox brings up yesno box
        Dim dgCB As String
        dgCB = DataGridView1("Insurance", e.RowIndex).Value.ToString()
        If dgCB = False Then
                 'code if insurance becomes checked
        ElseIf dgCB = True Then
                  'code if insurance becomes unchecked

        End If


To clear things up:
dgCB is my variable string that contains the boolean variable of Insurance
DataGridView1 is my datagridview
 
anyway I could handle a specific column click within the datagridview?
You can do different things depending on the e.ColumnIndex.

A Boolean is not a String, it is a Boolean. Turn on Option Strict.
 
I got it to work setting up two variables: one called value, and one called dgCB, Value was set up to equal true or false like dgCB was previously set up to be, dgCB was setup to determine if the Insurance checkbox was actually the one checked or not by using .EditingCellValueChanged, I then had an If statement that said

(pseudo)
If Value = false and dgCB.EditingCellValueChanged Then
'code

I dimmed dgCB as a New datagridviewcheckbox cell
dgCB = CType(DataGridView1("Insurance", e.RowIndex), DataGridViewCheckBoxCell)

Thanks for all your help!
 
Back
Top