none
Datagridview Checkbox column make only one checkbox checkable RRS feed

  • Question

  • Hallo,

    I have a check box column (Boolean property) in Datagridview, comes from a Bindinglist with bindigsource . I want to make only one object can have the property True. Any idea?

    Thanks

     

    Sunday, October 25, 2020 8:52 PM

Answers

  • Hi

    One way.  Beware if you have a very large number of rows.

    This example sets the cell itself check/uncheck as well as the checkbox.

    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim dt As New DataTable("Freddy")
    	Dim BS As New BindingSource
    	Dim r As New Random
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		With dt
    			.Columns.Add("Boo", GetType(Boolean))
    			.Columns.Add("St1", GetType(Integer))
    			.Columns.Add("St2", GetType(Integer))
    
    			For i As Integer = 0 To 19
    				.Rows.Add(False, i, i * 3)
    			Next
    		End With
    		BS.DataSource = dt
    		DataGridView1.DataSource = BS
    	End Sub
    	Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
    		If e.RowIndex < 0 Then Exit Sub
    		If e.ColumnIndex = DataGridView1.Columns("Boo").Index Then
    			Dim boo As Boolean = CBool(DataGridView1("Boo", e.RowIndex).Value)
    			DataGridView1("Boo", e.RowIndex).Value = boo
    			BS.EndEdit()
    			DataGridView1.RefreshEdit()
    		End If
    	End Sub
    	Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
    		If e.RowIndex < 0 Then Exit Sub
    		Select Case e.ColumnIndex
    			Case DataGridView1.Columns("Boo").Index
    				Dim boo As Boolean = CBool(DataGridView1("Boo", e.RowIndex).Value)
    				DataGridView1("Boo", e.RowIndex).Value = Not boo
    				BS.EndEdit()
    				DataGridView1.RefreshEdit()
    		End Select
    		ClearThem(e.RowIndex)
    	End Sub
    	Sub ClearThem(r As Integer)
    		For i As Integer = 0 To DataGridView1.Rows.Count - 1
    			If Not i = r Then DataGridView1("Boo", i).Value = False
    		Next
    	End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by Shan1986 Tuesday, October 27, 2020 10:15 AM
    Sunday, October 25, 2020 10:29 PM
  • Hello,

    See the following GitHub repository project.

    The Available column can have only one row checked and uses a BindingSource.


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Shan1986 Tuesday, October 27, 2020 10:15 AM
    Sunday, October 25, 2020 11:02 PM
    Moderator

All replies

  • Hi

    One way.  Beware if you have a very large number of rows.

    This example sets the cell itself check/uncheck as well as the checkbox.

    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim dt As New DataTable("Freddy")
    	Dim BS As New BindingSource
    	Dim r As New Random
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		With dt
    			.Columns.Add("Boo", GetType(Boolean))
    			.Columns.Add("St1", GetType(Integer))
    			.Columns.Add("St2", GetType(Integer))
    
    			For i As Integer = 0 To 19
    				.Rows.Add(False, i, i * 3)
    			Next
    		End With
    		BS.DataSource = dt
    		DataGridView1.DataSource = BS
    	End Sub
    	Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
    		If e.RowIndex < 0 Then Exit Sub
    		If e.ColumnIndex = DataGridView1.Columns("Boo").Index Then
    			Dim boo As Boolean = CBool(DataGridView1("Boo", e.RowIndex).Value)
    			DataGridView1("Boo", e.RowIndex).Value = boo
    			BS.EndEdit()
    			DataGridView1.RefreshEdit()
    		End If
    	End Sub
    	Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
    		If e.RowIndex < 0 Then Exit Sub
    		Select Case e.ColumnIndex
    			Case DataGridView1.Columns("Boo").Index
    				Dim boo As Boolean = CBool(DataGridView1("Boo", e.RowIndex).Value)
    				DataGridView1("Boo", e.RowIndex).Value = Not boo
    				BS.EndEdit()
    				DataGridView1.RefreshEdit()
    		End Select
    		ClearThem(e.RowIndex)
    	End Sub
    	Sub ClearThem(r As Integer)
    		For i As Integer = 0 To DataGridView1.Rows.Count - 1
    			If Not i = r Then DataGridView1("Boo", i).Value = False
    		Next
    	End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by Shan1986 Tuesday, October 27, 2020 10:15 AM
    Sunday, October 25, 2020 10:29 PM
  • Hello,

    See the following GitHub repository project.

    The Available column can have only one row checked and uses a BindingSource.


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Shan1986 Tuesday, October 27, 2020 10:15 AM
    Sunday, October 25, 2020 11:02 PM
    Moderator
  • thanks for both answers ..
    Tuesday, October 27, 2020 10:17 AM
  • Hi

    One way.  Beware if you have a very large number of rows.

    This example sets the cell itself check/uncheck as well as the checkbox.

    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim dt As New DataTable("Freddy")
    	Dim BS As New BindingSource
    	Dim r As New Random
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		With dt
    			.Columns.Add("Boo", GetType(Boolean))
    			.Columns.Add("St1", GetType(Integer))
    			.Columns.Add("St2", GetType(Integer))
    
    			For i As Integer = 0 To 19
    				.Rows.Add(False, i, i * 3)
    			Next
    		End With
    		BS.DataSource = dt
    		DataGridView1.DataSource = BS
    	End Sub
    	Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
    		If e.RowIndex < 0 Then Exit Sub
    		If e.ColumnIndex = DataGridView1.Columns("Boo").Index Then
    			Dim boo As Boolean = CBool(DataGridView1("Boo", e.RowIndex).Value)
    			DataGridView1("Boo", e.RowIndex).Value = boo
    			BS.EndEdit()
    			DataGridView1.RefreshEdit()
    		End If
    	End Sub
    	Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
    		If e.RowIndex < 0 Then Exit Sub
    		Select Case e.ColumnIndex
    			Case DataGridView1.Columns("Boo").Index
    				Dim boo As Boolean = CBool(DataGridView1("Boo", e.RowIndex).Value)
    				DataGridView1("Boo", e.RowIndex).Value = Not boo
    				BS.EndEdit()
    				DataGridView1.RefreshEdit()
    		End Select
    		ClearThem(e.RowIndex)
    	End Sub
    	Sub ClearThem(r As Integer)
    		For i As Integer = 0 To DataGridView1.Rows.Count - 1
    			If Not i = r Then DataGridView1("Boo", i).Value = False
    		Next
    	End Sub
    End Class


    Regards Les, Livingston, Scotland

    Hallo Les, There is a problem,  even if i select the checkbox or not or if i go to another cell in another  column it de-selects the previous .

    Tuesday, October 27, 2020 11:57 AM
  • Hi

    My apologies.  I placed the line 'ClearThem(e.RowIndex)' in the wrong place.  If you move that line up one to just before the End Select then it should work OK.


    Regards Les, Livingston, Scotland

    Tuesday, October 27, 2020 12:48 PM
  • thanks Leshay. It worked.
    Wednesday, October 28, 2020 4:31 PM