dimanche 21 juin 2015

Select only one checkbox from multiple checkbox in a row from DataGridView

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

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

I need to use checkbox here not radiobutton.

Can someone give advice of what I did wrong here ?

Thanks




Aucun commentaire:

Enregistrer un commentaire