none
Tick checkbox in one datagrid isn't being tick in another datagrid RRS feed

  • Question

  • Hello

    I've been having an issue with datagrid lately, especially with check boxes

    My issue is that I have 2 forms, both with datagrid, and both have checkboxes that need to be ticked. When I tick one checkbox in my main form, the other checkbox won't tick. 

    I've been testing with this code but have no luck.

     Private Sub btnWVValidate_Click(sender As Object, e As EventArgs) Handles btnWVValidate.Click
                For i As Integer = 0 To GVWVorders.Rows.Count() - 1 Step +1
                Dim check As Boolean = DGVWVorders.Rows(i).Cells(3).Value
                Dim row As DataGridViewRow = DGVWVorders.Rows(i)
                If check = True Then
                    For j As Integer = 0 To frmCustomerOrderStatus.DGVOrderDetails.Rows.Count() - 1 Step +1
                        Dim check2 As Boolean = frmCustomerOrderStatus.DGVOrderDetails.Rows(j).Cells(1).Value
                        If frmCustomerOrderStatus.DGVOrderDetails.Rows(j).Cells(1).Value = True Then
                        Else
                            frmCustomerOrderStatus.DGVOrderDetails.Rows(j).Cells(1).Value = False
                        End If
                    Next
                End If
            Next
        End Sub
    End Class

    Please help, Vincent

    EDIT

    I've been trying for a bit and changed the coding around:

    Private Sub btnWVValidate_Click(sender As Object, e As EventArgs) Handles btnWVValidate.Click
            For i As Integer = 0 To DGVWVorders.Rows.Count() - 1 Step +1
                Dim check As Boolean = DGVWVorders.Rows(i).Cells(3).Value
                Dim row As DataGridViewRow = DGVWVorders.Rows(i)
                If check = True Then
                    For j As Integer = 0 To frmCustomerOrderStatus.DGVOrderDetails.Rows.Count() - 1 Step +1
                        Dim check2 As Boolean = frmCustomerOrderStatus.DGVOrderDetails.Rows(j).Cells(1).Value
                        If check2 = True Then
                            frmCustomerOrderStatus.DGVOrderDetails.Rows(j).Cells(1) = True
                        Else
                            frmCustomerOrderStatus.DGVOrderDetails.Rows(j).Cells(1) = False
                        End If
                    Next
                End If
            Next
        End Sub
    End Class

    However, both 

     frmCustomerOrderStatus.DGVOrderDetails.Rows(j).Cells(1) = True
    

    and

     frmCustomerOrderStatus.DGVOrderDetails.Rows(j).Cells(1) = False

    come up with errors with the values not accepting the boolean.

    Is there an error with the way I've structured it? 

    - Vincent


    • Edited by Vin.1056 Sunday, August 5, 2018 3:37 AM Tried to fix coding
    Sunday, August 5, 2018 3:26 AM

All replies

  • Vin,

    What is the error message?

    Did you leave out the .value from your statement ie:

       frmCustomerOrderStatus.DGVOrderDetails.Rows(j).Cells(1).value = True


    BTW, if you are trying to set the checkboxes the same in the second dgv then maybe this will be easier?

            DataGridView1.Rows(0).Cells(0).Value = DataGridView2.Rows(0).Cells(0).Value



    Sunday, August 5, 2018 11:55 AM
  • One way to reduce the complexity of the current logic is to simply get all checked rows followed by looping through the list and check against the other DataGridView in the other form.

    The following at first glance may seem like a lot of code compared to what you are using yet consider the following. If the two code blocks resided in a class project that could be used by the current project and future projects simply by adding that class project to a Visual Studio solution then it should be seen as a code saver while using it once of course would appear as way too much code. I wrote this over eight years ago and have used it countless times.

    Add this class to the project which is required for the second code block,

    Public Class CheckedRow
        Public Property Row() As DataGridViewRow
        Public Property RowIndex() As Integer
    End Class
    


    Add this code module to your project.

    Public Module DataGridViewLanguageExtensions
        ''' <summary>
        ''' Get checked rows in a DataGridView by column name and
        ''' status of a specific DataGridViewColumn.
        ''' </summary>
        ''' <param name="dgv">DataGridView to work on</param>
        ''' <param name="pColumnName">The DataGridViewCheckBoxColumn name</param>
        ''' <param name="pCheckedState">State: true or false where the default is true</param>
        ''' <returns>List of CheckedRow</returns>
        <DebuggerStepThrough()>
        <Runtime.CompilerServices.Extension()>
        Public Function CheckedRows(
                                    dgv As DataGridView,
                                    pColumnName As String,
                                    Optional pCheckedState As Boolean = True) As List(Of CheckedRow)
    
            Return dgv.Rows.Cast(Of DataGridViewRow)().
                Select(Function(item, index) New CheckedRow With {.Row = item, .RowIndex = index}).
                Where(Function(item) Not item.Row.IsNewRow AndAlso CBool(item.Row.Cells(pColumnName).Value) = pCheckedState).
                ToList()
        End Function
    
    End Module

    Usage where the variable results has each DataGridView row that is checked and the row index of each checked row excluding the new row if the DataGridView is setup for allowing users to add new rows.

    The line with Console.WriteLine you take item.RowIndex, compare it to the other DataGridView via 

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim results As List(Of CheckedRow) = DataGridView1.CheckedRows("Column1")
        For Each item As CheckedRow In results
            Console.WriteLine($"{item.RowIndex}")
        Next
    
    End Sub

    Comparing could look like this

    If CBool(OtherDataGridView.Rows(item.RowIndex).Cells(1).Value) = True Then
    
    End If


    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

    Sunday, August 5, 2018 1:35 PM
    Moderator