none
Check first checkbox in datagridview RRS feed

  • Question

  • Hello,

    I place a checkbox's in a datagridview with some data , and you can only select one at the time.

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

    This for only one at the time.

                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

    But how do i check the checkbox of the first row  after formload?

    Sunday, December 30, 2018 10:11 PM

Answers

  • As follows

    DataGridView1.Rows(0).Cells("chk").Value = True

    Even though you indicated form load there must be another event your working with too as form load does not have any arguments for e.ColumnIndex.

    Here is an example for allowing only one row to be checked. Columns where added via the designer.

    ''' <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.CellContentClick, AddressOf dataGridView_CellContentClick
            columnIndex = DataGridView1.Columns("AvailableColumn").Index
        End Sub
        Private Sub dataGridView_CellContentClick(sender As Object, e As DataGridViewCellEventArgs)
            Dim dataGridView = CType(sender, DataGridView)
    
            If e.ColumnIndex = columnIndex Then
                dataGridView.EndEdit()
    
                Dim isChecked = CBool(dataGridView(e.ColumnIndex, e.RowIndex).Value)
    
                If isChecked Then
                    For Each row As DataGridViewRow In dataGridView.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
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim result = DataGridView1.Rows.Cast(Of DataGridViewRow).FirstOrDefault(Function(row) CBool(row.Cells("AvailableColumn").Value))
            If result IsNot Nothing Then
                MessageBox.Show($"{result.Cells("RoomColumn").Value}")
            Else
                MessageBox.Show("No checked rooms")
            End If
    
        End Sub
    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



    Monday, December 31, 2018 12:01 AM
    Moderator
  • Hi,

    use real-time trigger events

      Private Sub DataGridView1_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
    
            If e.ColumnIndex = 0 AndAlso e.RowIndex >= 0 Then
                If DataGridView1.Rows(0).Cells("chk").EditedFormattedValue = True Then
                    MessageBox.Show("11")
                End If
            End If
    
        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.

    • Marked as answer by KeesBlunder Tuesday, January 8, 2019 10:47 AM
    Tuesday, January 1, 2019 6:45 AM

All replies

  • As follows

    DataGridView1.Rows(0).Cells("chk").Value = True

    Even though you indicated form load there must be another event your working with too as form load does not have any arguments for e.ColumnIndex.

    Here is an example for allowing only one row to be checked. Columns where added via the designer.

    ''' <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.CellContentClick, AddressOf dataGridView_CellContentClick
            columnIndex = DataGridView1.Columns("AvailableColumn").Index
        End Sub
        Private Sub dataGridView_CellContentClick(sender As Object, e As DataGridViewCellEventArgs)
            Dim dataGridView = CType(sender, DataGridView)
    
            If e.ColumnIndex = columnIndex Then
                dataGridView.EndEdit()
    
                Dim isChecked = CBool(dataGridView(e.ColumnIndex, e.RowIndex).Value)
    
                If isChecked Then
                    For Each row As DataGridViewRow In dataGridView.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
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim result = DataGridView1.Rows.Cast(Of DataGridViewRow).FirstOrDefault(Function(row) CBool(row.Cells("AvailableColumn").Value))
            If result IsNot Nothing Then
                MessageBox.Show($"{result.Cells("RoomColumn").Value}")
            Else
                MessageBox.Show("No checked rooms")
            End If
    
        End Sub
    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



    Monday, December 31, 2018 12:01 AM
    Moderator
  • Show how you 'load' your datagirdview. Assuming that you use a datatable as a datasource, its probably easier to add a boolean column to that and not add a DGVCheckBoxCol. Then in your requirement you could instead change the value of the bindingsource/datatable and not mess with DataGridView methods at all.

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

    Monday, December 31, 2018 3:40 AM
  • Hi,

    use real-time trigger events

      Private Sub DataGridView1_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
    
            If e.ColumnIndex = 0 AndAlso e.RowIndex >= 0 Then
                If DataGridView1.Rows(0).Cells("chk").EditedFormattedValue = True Then
                    MessageBox.Show("11")
                End If
            End If
    
        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.

    • Marked as answer by KeesBlunder Tuesday, January 8, 2019 10:47 AM
    Tuesday, January 1, 2019 6:45 AM
  • Hello,

    Has this issue been resolved? 


    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

    Tuesday, January 8, 2019 10:14 AM
    Moderator
  • Oh sorry Karen ,

    I thought i already put this on solved

    Tuesday, January 8, 2019 10:45 AM