Search code examples
vb.netcheckboxdatagridview

Clear TextBoxes when uncheck CheckBox in Data DataGridView


With this a CheckBox in a DataGridView2 in FormLoad

  Dim chk1 As DataGridViewCheckBoxColumn = New DataGridViewCheckBoxColumn()
  DataGridView2.Columns.Add(chk1)
  chk1.HeaderText = "Keuze"
  chk1.Name = "chk"

When select a CheckBox filling some textboxes

 Private Sub DataGridView2_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView2.CellContentClick
     If e.ColumnIndex = 0 AndAlso e.RowIndex >= 0 Then
         For Each row As DataGridViewRow In DataGridView2.Rows
             row.Cells(e.ColumnIndex).Value = False
         Next
     End If
     Dim senderGrid As DataGridView = sender
     Dim data = senderGrid.Rows(e.RowIndex).DataBoundItem

     If senderGrid.Columns(e.ColumnIndex).GetType() Is GetType(DataGridViewCheckBoxColumn) And e.RowIndex >= 0 Then
         TBterug.Text = DataGridView2.CurrentRow.Cells("Todo").Value.ToString()
         CBbelangrijkterug.Text = DataGridView2.CurrentRow.Cells("Belangrijk").Value.ToString()
         Id.Text = DataGridView2.CurrentRow.Cells("Id").Value.ToString()
         LBdelete.Visible = True
         LBuncheck.Visible = True
     End If

 End Sub

But when i Uncheck the CheckBox i want to Empty the TextBoxes , what do i have to add to make this work ?

I tryed this but no luck Check/Uncheck a checkbox on datagridview


Solution

  • Thanks for the answer dr.null i will take a look at it.

    But for now. Found a solution here , and made it work with a ButtonClick. Used this chk.Value = chk.FalseValue

    Check/Uncheck a checkbox on datagridview

    In FormLoad

    Dim CheckboxColumn As DataGridViewCheckBoxColumn = New DataGridViewCheckBoxColumn()
    CheckboxColumn.TrueValue = True
    CheckboxColumn.FalseValue = False
    CheckboxColumn.Width = 10
    DataGridView2.Columns.Add(CheckboxColumn)
    DataGridView2.Rows.Add(1)
    CheckboxColumn.HeaderText = "Keuze"
    CheckboxColumn.Name = "chk"
    

    And

    Private Sub DataGridView2_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView2.CellContentClick
         If e.ColumnIndex = 0 AndAlso e.RowIndex >= 0 Then
             For Each row As DataGridViewRow In DataGridView2.Rows
                 row.Cells(e.ColumnIndex).Value = False
             Next
         End If
         Dim senderGrid As DataGridView = sender
         Dim data = senderGrid.Rows(e.RowIndex).DataBoundItem
    
         If senderGrid.Columns(e.ColumnIndex).GetType() Is GetType(DataGridViewCheckBoxColumn) And e.RowIndex >= 0 Then
             TBterug.Text = DataGridView2.CurrentRow.Cells("Todo").Value.ToString()
             CBbelangrijkterug.Text = DataGridView2.CurrentRow.Cells("Belangrijk").Value.ToString()
             Id.Text = DataGridView2.CurrentRow.Cells("Id").Value.ToString()
             BTdelete.Visible = True
             BTreturn.Visible = True
    
         End If
    
    End Sub
    

    Added a Button

     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles BTreturn.Click
         For Each row As DataGridViewRow In DataGridView2.Rows
             Dim chk As DataGridViewCheckBoxCell = CType(row.Cells(0), DataGridViewCheckBoxCell)
    
             If chk.Value = chk.FalseValue OrElse chk.Value Is Nothing Then
    
             Else
    
                 chk.Value = chk.FalseValue
                 TBterug.Text = ""
                 CBbelangrijkterug.Text = ""
                 Id.Text = ""
                 TBnieuw.Text = ""
                 CBbelangrijk.Text = ""
                 Id.Text = ""
                 BTdelete.Visible = False
                 BTreturn.Visible = False
             End If
         Next
    
         DataGridView2.EndEdit()
     End Sub