none
Datagridview checkbox cell does not react directly RRS feed

  • Question

  • Hello,

    I created a datagridview application. I added a checkbox column on it but the datagridviewrow is "editing" when I changed the check. How can I directly pass the changes after the check changes?

    Wednesday, May 30, 2018 11:04 AM

All replies

  • You could handle the CellValueChanged event, and if the cell index is in the checkbox column index, then call EndEdit on the DataGridView.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, May 30, 2018 11:11 AM
    Moderator
  • You know the windows forms controls refresh method which also applies to the datagridview?

    https://msdn.microsoft.com/en-us/library/system.windows.forms.control.refresh(v=vs.110).aspx


    Success
    Cor


    Wednesday, May 30, 2018 11:15 AM
  • As an aside, I recognize that icon on your import button...  If you are working with 2009/2012, I may be able to offer some tips and pointers.  Feel free to contact me directly if interested.  reedkimble at live dot com.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, May 30, 2018 11:16 AM
    Moderator
  • Hello,

    Here is an example that uses mocked data

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DataGridView1.Rows.Add(New Object() {False, "Smith"})
            DataGridView1.Rows.Add(New Object() {False, "Jones"})
            DataGridView1.Rows.Add(New Object() {False, "Adams"})
            DataGridView1.Rows.Add(New Object() {False, "Holmes"})
            DataGridView1.Rows.Add(New Object() {False, "Simpson"})
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim CheckedRows = DataGridView1.GetCheckedRows("Checked")
    
            If CheckedRows.Count > 0 Then
    
    
                For Each row As DataGridViewRow In CheckedRows
                    Console.WriteLine(row.Cells(1).Value)
                    If row IsNot Nothing Then
                        row.CloneRowWithValues(DataGridView1, "Checked")
                    End If
                Next
    
            End If
    
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            For i As Integer = DataGridView1.Rows.Count - 1 To 0 Step -1
                If CBool(DataGridView1.Rows(i).Cells("Checked").Value) Then
                    DataGridView1.Rows.RemoveAt(i)
                End If
            Next
        End Sub
    
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            MessageBox.Show(DataGridView1.GetCheckedRows("Checked").Count.ToString)
        End Sub
    
        Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
            If DataGridView1.CurrentCell.ColumnIndex = 0 Then
                DataGridView1.EndEdit()
                Console.WriteLine(DataGridView1.CurrentRow.Cells("Lastname").Value.ToString)
            End If
    
        End Sub
    End Class
    Module Module1
        <System.Diagnostics.DebuggerStepThrough()> _
        <Runtime.CompilerServices.Extension()> _
        Public Function GetCheckedRows(ByVal GridView As DataGridView, ByVal ColumnName As String) As List(Of DataGridViewRow)
            Dim Temp = (From Rows In GridView.Rows.Cast(Of DataGridViewRow)() Where Not Rows.IsNewRow).ToList
            Return (From SubRows In Temp Where CBool(SubRows.Cells(ColumnName).Value) = True).ToList
        End Function
        <System.Diagnostics.DebuggerStepThrough()> _
        <System.Runtime.CompilerServices.Extension()> _
        Public Sub CloneRowWithValues(ByVal SingleRow As DataGridViewRow, ByVal Target As DataGridView, ByVal ProcessColumnName As String)
            Dim Results As DataGridViewRow = CType(SingleRow.Clone(), DataGridViewRow)
    
            For Row As Int32 = 0 To SingleRow.Cells.Count - 1
                Results.Cells(Row).Value = SingleRow.Cells(Row).Value
            Next
    
            Target.Rows.Add(Results)
            Results.Cells(ProcessColumnName).Value = False
            SingleRow.Cells(ProcessColumnName).Value = False
        End Sub
    
    End Module

    For more on working with DataGridViewCheckBox

    Basics to advance working with DataGridView Checkbox columns


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Wednesday, May 30, 2018 11:18 AM
    Moderator
  • Hello,

    I created a datagridview application. I added a checkbox column on it but the datagridviewrow is "editing" when I changed the check. How can I directly pass the changes after the check changes?

    Hi MechancalEngineer,

    You said that you want to directly pass the changes after the check changes, what it means? Can you describe more detailed info about you want to do here.

    According to your picture, I find when you check one checkbox in datagridview, you just select the one column, so you want to select the entire datagridview row when you check checkbox in datagridview? If yes, you can set datagridview's selectionmode as fullrowselect.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, May 31, 2018 2:54 AM
    Moderator
  • I think you maybe looking more for something like this

        Private Sub DGVFMLDist_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles DataGridView1.CurrentCellDirtyStateChanged
            DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
        End Sub
    
        Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
            With DataGridView1
                If .CurrentCell.OwningColumn.Name = "YourChkBoxColumnName" Then
                    'Do Stuff
                End If
            End With
        End Sub


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Thursday, May 31, 2018 2:51 PM