1

I'm using DataGridView with DataSource from TableAdapter and add 5 unbound columns with type DataGridViewCheckBoxColumn .

They are : "lima","empat","tiga","dua","satu"

What I need to do, in the same row when checkbox "lima" is selected, the rest checkboxes become unselected or only can select one checkbox in the same row

Here's what I wrote down in CellValueChanged event :

   Private Sub DataGridView3_CellValueChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView3.CellValueChanged
        If DataGridView3.CurrentRow IsNot Nothing Then
            Dim SelectedColumnName = DataGridView3.Columns(DataGridView3.CurrentCellValue.ColumnIndex).Name

            If e.ColumnIndex = DataGridView3.Columns("lima").Index OrElse _
               e.ColumnIndex = DataGridView3.Columns("empat").Index OrElse _
               e.ColumnIndex = DataGridView3.Columns("tiga").Index OrElse _
               e.ColumnIndex = DataGridView3.Columns("dua").Index OrElse _
               e.ColumnIndex = DataGridView3.Columns("satu").Index Then

                Dim Row = CType((CType(DataGridView3.DataSource.Current, DataRowView)).Row, DataRow)

                If SelectedColumnName = "lima" Then
                    If DataGridView3.CurrentRowCellValue("lima") = "True" Then
                        Row.Item("empat") = False
                        Row.Item("tiga") = False
                        Row.Item("dua") = False
                        Row.Item("satu") = False
                    End If

                   'and so..on 

                ElseIf SelectedColumnName = "satu" Then
                    If DataGridView3.CurrentRowCellValue("satu") = "True" Then
                        Row.Item("lima") = False
                        Row.Item("empat") = False
                        Row.Item("tiga") = False
                        Row.Item("dua") = False
                    End If

                End If

                DataGridView3.CurrentCell = DataGridView3(e.ColumnIndex, e.RowIndex)
            End If
            End If

    End Sub

And this is the functions for CurrentCell and CurrentRowCellValue in module

<System.Diagnostics.DebuggerStepThrough()> _
<Runtime.CompilerServices.Extension()> _
Function CurrentRowCellValue(ByVal sender As DataGridView, ByVal ColumnName As String) As String
    Dim Result As String = ""
    If Not sender.Rows(sender.CurrentRow.Index).Cells(ColumnName).Value Is Nothing Then
        Result = sender.Rows(sender.CurrentRow.Index).Cells(ColumnName).Value.ToString
    End If
    Return Result
End Function

<System.Diagnostics.DebuggerStepThrough()> _
<Runtime.CompilerServices.Extension()> _
Function CurrentCellValue(ByVal sender As DataGridView) As DataGridViewCell
    Return sender(sender.Columns(sender.CurrentCell.ColumnIndex).Index, sender.CurrentRow.Index)
End Function

and this is from CurrentCellDirtyStateChanged event

Private Sub DataGridView3_CurrentCellDirtyStateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView3.CurrentCellDirtyStateChanged
    If TypeOf DataGridView3.CurrentCell Is DataGridViewCheckBoxCell Then
        DataGridView3.CommitEdit(DataGridViewDataErrorContexts.Commit)
    End If
End Sub

I already set the DataGridView's ReadOnly property to False. The problem is that I still can check all the checkboxes in the same row.

enter image description here

Can someone give advice of what I did wrong here ?

Thanks

2 Answers 2

1

Some tips:

  • See this link concerning CellValuechanged event when working with checkBoxs
  • If they are unbound columns, you can't change values via the DataSource.Current row: table has no definition for lima, empat, ...

This workaround (using CellContentClick) works for me (no need to extension methods and CurrentCellDirtyStateChanged event):


 Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView3.CellContentClick

        Dim checkboxIndexes As New List(Of Integer)
        checkboxIndexes.Add(DataGridView3.Columns("lima").Index)
        checkboxIndexes.Add(DataGridView3.Columns("empat").Index)
        checkboxIndexes.Add(DataGridView3.Columns("tiga").Index)
        checkboxIndexes.Add(DataGridView3.Columns("dua").Index)
        checkboxIndexes.Add(DataGridView3.Columns("satu").Index)

        If checkboxIndexes.Contains(e.ColumnIndex) Then
        'check for false value because event occurs before row is validated
            If DataGridView3.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = False Then
                For Each index In checkboxIndexes
                    If index <> e.ColumnIndex Then
                        DataGridView3.Rows(e.RowIndex).Cells(index).Value = False
                    End If
                Next
            End If
        End If
    End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, It works this way...! Thanks for code and link reference :)
0

I made an improve to the code added before in order to achieve my goals of checking checkboxes of the same row in different columns. It works for me.

Private Sub Grilla_CellClick(sender As Object, e As Telerik.WinControls.UI.GridViewCellEventArgs) Handles Grilla.CellClick

    Dim checkboxIndexes As New List(Of Integer)
    checkboxIndexes.Add(Grilla.Columns("Select").Index)
    checkboxIndexes.Add(Grilla.Columns("Select1").Index)


    If checkboxIndexes.Contains(e.ColumnIndex) Then
        'check for false value because event occurs before row is validated
        If Grilla.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = False Then
            For Each index In checkboxIndexes
                If index <> e.ColumnIndex Then
                    'Do nothing here
                Else
                    If Grilla.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = False Then
                        Grilla.Rows(e.RowIndex).Cells(index).Value = True
                    Else
                        Grilla.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = False
                    End If

                End If
            Next

        Else

            If Grilla.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = True Then
                For Each index In checkboxIndexes
                    If index <> e.ColumnIndex Then
                        'Do nothing here
                    Else
                        If Grilla.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = False Then
                            Grilla.Rows(e.RowIndex).Cells(index).Value = True
                        Else
                            Grilla.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = False
                        End If

                    End If
                Next
            End If

        End If
    End If

    Grilla.TableElement.Update(GridUINotifyAction.DataChanged)

End Sub

3 Comments

Can you, please, explain briefly the improvement?
Sorry if i wasn´t clear enought, i meant that i adapted it to my needs, these were to be able to check and uncheck, in this case, 2 checkboxes in the same row independently between them.
Is it an answer for the OP's question?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.