none
Combine grouping and only 1 checkbox checked in datagridview problem RRS feed

  • Question

  • I have 2 code's that works

    Only 1 checkbox checked in a datagridview

    Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
          
                If e.ColumnIndex = 0 AndAlso e.RowIndex >= 0 Then
                    For Each row As DataGridViewRow In DataGridView1.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
    
                  '''' do stuf
                End If
    End Sub

    And grouping ( by making text white)

     Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
            Try
                If e.RowIndex > 0 And e.ColumnIndex = 0 Then
                    If DataGridView1.Item(0, e.RowIndex - 1).Value = e.Value Then
                        e.Value = ""
                    ElseIf e.RowIndex < DataGridView1.Rows.Count - 1 Then
                        DataGridView1.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.White
                    End If
                End If
            Catch ex As Exception
    
            End Try
        End Sub
     

    And in Formload

    Dim chk As DataGridViewCheckBoxColumn = New DataGridViewCheckBoxColumn() DataGridView1.Columns.Add(chk) chk.HeaderText = "Bewerken" chk.Name = "chk" DataGridView1.Columns("chk").DisplayIndex = 3



    Both work , but not at the same time.

    I can show the grouping and checkboxes oke i think it works, but you can select all checkboxes at the same time if you want.

    I want to select only 1

    so the first code is not working any more , i was trying to combine both but don't know how.

    Wednesday, February 20, 2019 8:46 PM

Answers

  • Hi,

    Your code:

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DataGridView1.ColumnCount = 3
            DataGridView1.Columns(0).Name = "Trip"
            DataGridView1.Columns(1).Name = "Product Name"
            DataGridView1.Columns(2).Name = "Product Price"
    
            Dim row As String() = New String() {"1", "Product 1", "1000"}
            DataGridView1.Rows.Add(row)
            row = New String() {"2", "Product 2", "2000"}
            DataGridView1.Rows.Add(row)
            row = New String() {"3", "Product 3", "1000"}
            DataGridView1.Rows.Add(row)
            row = New String() {"3", "Product 3", "1000"}
            DataGridView1.Rows.Add(row)
            row = New String() {"3", "Product 3", "1000"}
            DataGridView1.Rows.Add(row)
            row = New String() {"4", "Product 4", "1000"}
            DataGridView1.Rows.Add(row)
    
            Dim chk As DataGridViewCheckBoxColumn = New DataGridViewCheckBoxColumn()
            DataGridView1.Columns.Add(chk)
            chk.HeaderText = "Check Data"
            chk.Name = "Check Data"
    
            DataGridView1.Columns("Check Data").DisplayIndex = 1
        End Sub
    
        Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
            Try
                If e.RowIndex > 0 And e.ColumnIndex = 0 Then
                    If DataGridView1.Item(0, e.RowIndex - 1).Value = e.Value Then
                        e.Value = ""
                    ElseIf e.RowIndex < DataGridView1.Rows.Count - 1 Then
                        DataGridView1.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.White
                    End If
                End If
            Catch ex As Exception
    
            End Try
        End Sub
    
        Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
            If e.ColumnIndex = 3 AndAlso e.RowIndex >= 0 Then
                For Each row As DataGridViewRow In DataGridView1.Rows
                    row.Cells(e.ColumnIndex).Value = False
                Next
            End If
        End Sub
    End Class
    

    effect :

    Do you still have any problem?

    Best Regards,

    Alex


    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.

    • Marked as answer by KeesBlunder Wednesday, February 27, 2019 9:59 PM
    Wednesday, February 27, 2019 5:13 AM

All replies

  • Hello,

    I have a code sample for only allowing one row to be checked but is done with the DataSource set to a DataTable using ColumnChanged event of the DataTable.

    https://code.msdn.microsoft.com/Bascis-to-advance-working-7b757af3?redir=0

    Code is in

    Don't have a solution for the grouping other than using a special DataGridView (which I have) that groups by the first column

    https://1drv.ms/u/s!AtGAgKKpqdWjg3id2byeFid59aR6


    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, February 20, 2019 8:56 PM
    Moderator
  • Hi,

    I think the second piece of code can also implement the first piece of code function.

    Imports System.Data.SqlClient
    Public Class Form1
        Dim dt As New DataTable
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim chk As DataGridViewCheckBoxColumn = New DataGridViewCheckBoxColumn
            DataGridView1.Columns.Add(chk)
    
            chk.HeaderText = "Bewerken"
    
            chk.Name = "chk"
            dt.Columns.Add("col1")
            dt.Columns.Add("col2")
            dt.Columns.Add("col3")
            dt.Rows.Add(New Object() {1, 2, 3})
            dt.Rows.Add(New Object() {4, 5, 6})
            dt.Rows.Add(New Object() {7, 8, 9})
            DataGridView1.DataSource = dt
            DataGridView1.Columns("chk").DisplayIndex = 3
    
        End Sub
    
        'Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        '    If e.ColumnIndex = 0 AndAlso e.RowIndex >= 0 Then
        '        For Each row As DataGridViewRow In DataGridView1.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
    
        '        '''' do stuf
        '    End If
        'End Sub
    
        Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
            Try
                If e.RowIndex >= 0 And e.ColumnIndex = 0 Then
                    If DataGridView1.Item(0, e.RowIndex).Value = e.Value Then
                        e.Value = False
                    Else
                        'DataGridView1.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Red
    
                    End If
                End If
            Catch ex As Exception
    
            End Try
        End Sub
    
    End Class
    

    Best Regards,

    Alex


    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, February 21, 2019 5:45 AM
  • Thanks Alex,

    The checkbox is working now , only 1 selected .

    Now only the grouping (making text White)

    This is what i mean , it looks like this now

    This is what i am lookig for 

    Thursday, February 21, 2019 7:28 AM
  • Hi,

    I am glad you have got your solution, we appreciated you shared us your solution and mark it as an answer.

    Best Regards,

    Alex


    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, February 21, 2019 8:01 AM
  • Alex,

    Thought i was finished but the grouping is not working anymore , the checkbox is fine now

    Thursday, February 21, 2019 9:38 AM
  • Hi,

    Do you want to the following effect?

     Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
            Try
                If e.RowIndex >= 0 And e.ColumnIndex = 0 Then
                    If DataGridView1.Item(0, e.RowIndex).Value = True Then
                        'e.Value = False
                        DataGridView1.Rows(e.RowIndex + 1).DefaultCellStyle.ForeColor = Color.White
                    Else
                        'DataGridView1.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Red
    
                    End If
                End If
            Catch ex As Exception
    
            End Try
    
        End Sub

    Best Regards,

    Alex


    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.

    Friday, February 22, 2019 6:27 AM
  • Alex,

    I mean only double or more of the same items in col nr 1 (Trip) , i made a small example so you can se what i mean.

    In the first column Trip there are 3 times nr 3 , make the other 2 white.

    It is only an example 

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
            DataGridView1.ColumnCount = 3
            DataGridView1.Columns(0).Name = "Trip"
            DataGridView1.Columns(1).Name = "Product Name"
            DataGridView1.Columns(2).Name = "Product Price"
    
            Dim row As String() = New String() {"1", "Product 1", "1000"}
            DataGridView1.Rows.Add(row)
            row = New String() {"2", "Product 2", "2000"}
            DataGridView1.Rows.Add(row)
            row = New String() {"3", "Product 3", "1000"}
            DataGridView1.Rows.Add(row)
            row = New String() {"3", "Product 3", "1000"}
            DataGridView1.Rows.Add(row)
            row = New String() {"3", "Product 3", "1000"}
            DataGridView1.Rows.Add(row)
            row = New String() {"4", "Product 4", "1000"}
            DataGridView1.Rows.Add(row)
    
            Dim chk As DataGridViewCheckBoxColumn = New DataGridViewCheckBoxColumn()
            DataGridView1.Columns.Add(chk)
            chk.HeaderText = "Check Data"
            chk.Name = "Check Data"
    
            DataGridView1.Columns("Check Data").DisplayIndex = 1
        End Sub

    Double or more of the same items in Column Trip only

    The original code 

    Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
            Try
                If e.RowIndex > 0 And e.ColumnIndex = 0 Then
                    If DataGridView1.Item(0, e.RowIndex - 1).Value = e.Value Then
                        e.Value = ""
                    ElseIf e.RowIndex < DataGridView1.Rows.Count - 1 Then
                        DataGridView1.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.White
                    End If
                End If
            Catch ex As Exception
    
            End Try
        End Sub



    • Edited by KeesBlunder Friday, February 22, 2019 7:34 AM
    Friday, February 22, 2019 7:34 AM
  • Hi,

    Your code:

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DataGridView1.ColumnCount = 3
            DataGridView1.Columns(0).Name = "Trip"
            DataGridView1.Columns(1).Name = "Product Name"
            DataGridView1.Columns(2).Name = "Product Price"
    
            Dim row As String() = New String() {"1", "Product 1", "1000"}
            DataGridView1.Rows.Add(row)
            row = New String() {"2", "Product 2", "2000"}
            DataGridView1.Rows.Add(row)
            row = New String() {"3", "Product 3", "1000"}
            DataGridView1.Rows.Add(row)
            row = New String() {"3", "Product 3", "1000"}
            DataGridView1.Rows.Add(row)
            row = New String() {"3", "Product 3", "1000"}
            DataGridView1.Rows.Add(row)
            row = New String() {"4", "Product 4", "1000"}
            DataGridView1.Rows.Add(row)
    
            Dim chk As DataGridViewCheckBoxColumn = New DataGridViewCheckBoxColumn()
            DataGridView1.Columns.Add(chk)
            chk.HeaderText = "Check Data"
            chk.Name = "Check Data"
    
            DataGridView1.Columns("Check Data").DisplayIndex = 1
        End Sub
    
        Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
            Try
                If e.RowIndex > 0 And e.ColumnIndex = 0 Then
                    If DataGridView1.Item(0, e.RowIndex - 1).Value = e.Value Then
                        e.Value = ""
                    ElseIf e.RowIndex < DataGridView1.Rows.Count - 1 Then
                        DataGridView1.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.White
                    End If
                End If
            Catch ex As Exception
    
            End Try
        End Sub
    
        Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
            If e.ColumnIndex = 3 AndAlso e.RowIndex >= 0 Then
                For Each row As DataGridViewRow In DataGridView1.Rows
                    row.Cells(e.ColumnIndex).Value = False
                Next
            End If
        End Sub
    End Class
    

    effect :

    Do you still have any problem?

    Best Regards,

    Alex


    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.

    • Marked as answer by KeesBlunder Wednesday, February 27, 2019 9:59 PM
    Wednesday, February 27, 2019 5:13 AM
  • Thanks Alex,

    Works great.

    Wednesday, February 27, 2019 9:59 PM