none
Uncheck other checkbox in datagridview RRS feed

  • Question

  • With this i put a checkbox in a datagridview

     Dim chk As DataGridViewCheckBoxColumn = New DataGridViewCheckBoxColumn()
            DataGridView1.Columns.Add(chk)
            chk.HeaderText = "Check Data"
            chk.Name = "chk"

    How do i uncheck the checkbox that i selected when i am selecting a different one ?

    I have to make sure that you can only select one.

    Wednesday, June 6, 2018 5:22 AM

Answers

  • Hi KeesBlunder,

    Do you want to get this?

    If yes, please take a look at my previous post code.

    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.

    • Marked as answer by KeesBlunder Wednesday, June 13, 2018 10:17 AM
    Friday, June 8, 2018 6:15 AM
    Moderator

All replies

  • How do i uncheck the checkbox that i selected when i am selecting a different one ?

    Probably the simplest way is to note the row that just got checked, then enumerate over all rows, clearing the column value to False, then set the column value for the selected row to True.

    An alternative is to store the row number of the last row that was set True, and clear the column value for that row to false before setting the checked row value to True.  However that will fail if you are adding rows to or removing rows from the DGV.

    Wednesday, June 6, 2018 5:32 AM
  • Here is an example that only allows one row checked.

    ''' <summary>
    ''' Columns for DataGridView added in the designer, could
    ''' have been added at runtime yet 99 percent of the time
    ''' that does not make sense.
    ''' 
    ''' The DataGridViewCheckBox column name is AvailableColumn
    ''' </summary>
    Public Class frmMainForm
    
        Private _bs As New BindingSource
        Private columnIndex As Integer = 0
        Private Sub Form_Load(sender As System.Object, e As EventArgs) Handles MyBase.Load
            DataGridView1.AutoGenerateColumns = False
            _bs.DataSource = GetMockedData()
            DataGridView1.DataSource = _bs
    
            AddHandler DataGridView1.CellValueChanged, AddressOf dataGridView_CellValueChanged
            columnIndex = DataGridView1.Columns("AvailableColumn").Index
        End Sub
        Private Sub dataGridView_CellValueChanged(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)
            If e.ColumnIndex = columnIndex Then
                Dim isChecked = CBool(DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value)
                If isChecked Then
                    For Each row As DataGridViewRow In DataGridView1.Rows
                        If row.Index <> e.RowIndex Then
                            row.Cells(columnIndex).Value = Not isChecked
                        End If
                    Next
                End If
            End If
        End Sub
        ''' <summary>
        ''' In a real app this would represent data read from
        ''' a database table.
        ''' </summary>
        ''' <returns></returns>
        Private Function GetMockedData() As DataTable
            Dim dt As New DataTable()
            dt.Columns.Add("Identifier", GetType(Integer))
            dt.Columns.Add("Room", GetType(String))
            dt.Columns.Add("RoomType", GetType(String))
            dt.Columns.Add("Rate", GetType(Decimal))
            dt.Columns.Add("Available", GetType(Boolean))
    
            dt.Rows.Add(10, "201A", "Suite", 98.99, False)
            dt.Rows.Add(20, "101A", "Suite", 120.99, False)
            dt.Rows.Add(30, "201B", "Suite", 99.99, False)
    
            dt.AcceptChanges()
    
            Return dt
    
        End Function
    End Class


    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, June 6, 2018 9:50 AM
    Moderator
  • Hello Karen,

    I used the code and it is better , but now there are 2 checked checkbox's.

    What ever one i select the are  always 2 checked and not 1.

    Wednesday, June 6, 2018 7:59 PM
  • Hello Karen,

    I used the code and it is better , but now there are 2 checked checkbox's.

    What ever one i select the are  always 2 checked and not 1.

    My apologies, here is one that does work and also provides a method to "get" the checked row if one is checked.

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


    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, June 6, 2018 10:43 PM
    Moderator
  • Hi keesBlunder,

    According to your description, you want to select only one data from datagridview, am I right? If yes, you can use the CellContentClick event of datagridview1 to make sure the check box can only select one.

    Imports System.Data
    Imports System.Data.SqlClient
    Public Class Form1
        Dim app_da As New System.Data.SqlClient.SqlDataAdapter
        Dim ds As New DataSet()
        Dim app_con As New SqlConnection
        Dim app_cmd As New SqlCommand
        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 = "Check Data"
            chk.Name = "chk"
            Try
                app_con = New SqlConnection("Data Source = (localdb)\MSSQLLocalDB; Integrated Security = True ;AttachDbFileName= C:\Users\alexl2\Desktop\DataBase\Andy.mdf")
                app_con.Open()
                app_da = New SqlDataAdapter("SELECT * FROM Student", app_con)
                app_da.Fill(ds)
                DataGridView1.DataSource = ds.Tables(0)
    
            Catch
    
            End Try
    
        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
        End Sub
    End Class
    

    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.

    • Proposed as answer by Stanly Fan Wednesday, June 13, 2018 7:09 AM
    Thursday, June 7, 2018 2:56 AM
    Moderator
  • Hello Karen ,

    i cannot get it to work

    Friday, June 8, 2018 2:02 AM
  • Hello Cherry

    The problem is that i can select more then one checkboxes in the datagridview.

    When i select another one it has to deselect the previus one.

    Friday, June 8, 2018 2:07 AM
  • Hi KeesBlunder,

    Do you want to get this?

    If yes, please take a look at my previous post code.

    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.

    • Marked as answer by KeesBlunder Wednesday, June 13, 2018 10:17 AM
    Friday, June 8, 2018 6:15 AM
    Moderator
  • Hello Karen ,

    i cannot get it to work

    Do you mean you can't get my demo to work or do you mean you can't implement what I gave into your project?

    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

    Friday, June 8, 2018 9:04 AM
    Moderator
  • Thanks this works.
    Wednesday, June 13, 2018 10:17 AM
  • Thanks Karen 

    Wednesday, June 13, 2018 10:18 AM