从DataGridView中连续选中多个复选框中的一个复选框

时间:2015-06-21 08:29:46

标签: vb.net winforms visual-studio-2010 checkbox datagridview

我将DataGridView与TableAdapter中的DataSource一起使用,并添加5个类型为DataGridViewCheckBoxColumn的未绑定列。

它们是:" lima"," empat"," tiga",&# 34; dua"" satu"

我需要做什么,在复选框" lima"的同一行。如果选中,其余复选框将取消选中,或者只能选择同一行中的一个复选框

这是我在CellValueChanged事件中记下的内容:

   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

这是模块

CurrentCellCurrentRowCellValue的功能
<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

这是来自CurrentCellDirtyStateChanged事件

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

我已经将DataGridView的ReadOnly属性设置为False。问题是我仍然可以检查同一行中的所有复选框。

http://oi57.tinypic.com/2hr0qbk.jpg

有人可以就我在这里做错的事情提出建议吗?

由于

2 个答案:

答案 0 :(得分:1)

一些提示:

  • 使用CellValuechanged
  • 时,请参阅有关checkBoxs事件的this link
  • 如果它们是未绑定的列,则无法通过DataSource.Current行更改值:表没有lima,empat,...的定义

此解决方法(使用CellContentClick)适用于我(无需扩展方法和CurrentCellDirtyStateChanged事件):

 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

答案 1 :(得分:0)

我对之前添加的代码进行了改进,以实现检查不同列中同一行的复选框的目标。它对我有用。

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