How Can Datagridview CheckBox unChecked

jewel 1,186 Reputation points
2022-01-10T08:32:01.803+00:00

I added a checkbox and a new column to the datagridview. The checkbox is being checked automatically when adding data to this column. If we delete this data thinking it is not necessary
The checkbox is still checked. I mean, once I give the data, the checkbox must be checked, but if I remove this data, how to unchecked the automatic checkbox
Goes.
I am making this effort on VB.net.
Thanks in advance to the experienced collaborators.

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim checkboxcolumn As New DataGridViewCheckBoxColumn()
checkboxcolumn.HeaderText = "*"
checkboxcolumn.Width = 20
checkboxcolumn.Name = "checkboxcolomn"
DataGridView1.Columns.Insert(0, checkboxcolumn)
End Sub

Sub display_data()
Dim cmd As New SqlCommand("select * from tbl_Pro", con)
Dim da As New SqlDataAdapter(cmd)
Dim dt As New DataTable
dt.Clear()
da.Fill(dt)
DataGridView1.DataSource = dt
dt.Columns.Add("Qty")
End Sub

Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
Dim cell1 As DataGridViewCheckBoxCell = DataGridView1.Rows(e.RowIndex).Cells(0)
cell1.Value = True
End Sub

Developer technologies VB
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 34,221 Reputation points Microsoft External Staff
    2022-01-11T02:44:06.56+00:00

    Hi @jewel ,
    You can set the value of the checkboxcolomn column by determining whether the NewColumn data of the current row is empty in DataGridView1.CellValueChanged.

    Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged  
        If DataGridView1("NewColumn", e.RowIndex).Value.ToString.Equals("") Then  
            DataGridView1("checkboxcolomn", e.RowIndex).Value = False  
        Else  
            DataGridView1("checkboxcolomn", e.RowIndex).Value = True  
        End If  
    End Sub  
    

    Hope the code above could be helpful.
    Best Regards.
    Jiachen Li

    ----------

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. LesHay 7,141 Reputation points
    2022-01-10T10:12:37.957+00:00

    Hi
    Difficult to understand what you are asking for. I think you need to have the checkbox auto check / uncheck depending on some other column - moybe!
    Anyway, here is some code that may help. This example explicitly loads the datatable but yours takes from a database - the ideas are the same.
    What I do here: set up the datatable with some columns, add some random data just for the example. Then, run through a loop to set the additional boolean column values according to the rules I have used (column [Two] empty = False, column [Two] not empty = True)

    After that, changing the value of a cell in column [Two] will auto check / uncheck the first (checkboxcolomn) column.

    Option Strict On
    Option Explicit On
    Public Class Form1
        Dim dt As New DataTable("Freddy")
        Dim r As New Random
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            With dt
                .Columns.Add("One", GetType(Integer))
                .Columns.Add("Two", GetType(String))
                .Columns.Add("Three", GetType(Double))
    
                For i As Integer = 0 To 99
                    .Rows.Add(r.Next(9, 99), IIf(r.Next(0, 2) = 1, "Freddy", String.Empty), r.NextDouble() * 99)
                Next
            End With
            DataGridView1.DataSource = dt
    
    
            Dim checkboxcolumn As New DataGridViewCheckBoxColumn()
            With checkboxcolumn
                .HeaderText = "*"
                .Width = 20
                .Name = "checkboxcolomn"
            End With
            DataGridView1.Columns.Insert(0, checkboxcolumn)
    
            For index As Integer = 0 To DataGridView1.Rows.Count - 1
                If Not index = DataGridView1.NewRowIndex Then
                    DataGridView1("checkboxcolomn", index).Value = Not DataGridView1("Two", index).Value.ToString = String.Empty
                End If
            Next
        End Sub
        Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
            DataGridView1("checkboxcolomn", e.RowIndex).Value = Not DataGridView1("Two", e.RowIndex).Value.ToString = String.Empty
        End Sub
    End Class
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.