现状
我显示一个包含自定义选项的输送机列表。当选择多个传送器时,我将不常见的选项显示为不确定状态,如下所示:

这说明传送器EB-1和EB-3 不具有相同的自定义选项(Test和Test2)。
目标
我希望用户能够单击不确定状态,并将其设置为false或true。当用户交互发生时,我不希望出现不确定状态。
带有ThreeState映射的选项加载
Public Enum TriState
Common
TurnedOn
TurnedOff
End Enum
Private Sub LoadCustomOptions()
ViewOptions.Table = DataSet1.CustomOptions
With dgvOptions
.DataSource = ViewOptions
'Add checkbox
Dim ChkBox As New DataGridViewCheckBoxColumn
ChkBox.Name = "ColChk"
ChkBox.HeaderText = ""
ChkBox.MinimumWidth = 20
ChkBox.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
ChkBox.ThreeState = True
ChkBox.TrueValue = TriState.TurnedOn
ChkBox.FalseValue = TriState.TurnedOff
ChkBox.IndeterminateValue = TriState.Common
ChkBox.ValueType = GetType(TriState)
ChkBox.DisplayIndex = 0
'Width
.Columns("Name").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
.Columns("Description").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
'Minimum Width
.Columns("Description").MinimumWidth = 150
.Columns.Add(ChkBox)
End With
End Sub尝试
以下是我尝试过的:
Private Sub dgvOptions_CellContentClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvOptions.CellContentClick
If e.RowIndex < 0 Then Exit Sub
If dgvOptions.Columns(e.ColumnIndex).Name = "ColChk" Then
If dgvOptions.Rows(e.RowIndex).Cells("ColChk").Value = TriState.TurnedOn Or dgvOptions.Rows(e.RowIndex).Cells("ColChk").Value = TriState.Common Then
dgvOptions.Rows(e.RowIndex).Cells("ColChk").Value = TriState.TurnedOff
Else
dgvOptions.Rows(e.RowIndex).Cells("ColChk").Value = TriState.TurnedOn
End If
End If
End Sub当用户单击自定义选项复选框时,我仍然会得到不确定状态。还有别的方法可以让我忽略这一点吗?
发布于 2014-11-04 16:13:19
终于弄明白了。请记住,我是在CurrentCellDirtyStateChanged中使用我的CurrentCellDirtyStateChanged Enum映射。
问题是,DataGridView.CurrentCell.Value属性(它是当前复选框设置的唯一链接)通常只有在用户通过移动到另一个单元格导致DataGridView将更改提交到复选框时才更新。不管用户单击同一复选框多少次,Value属性都不会更新。
Private Sub dgvOptions_CurrentCellDirtyStateChanged(ByVal sender As Object, ByVal e As EventArgs) Handles dgvOptions.CurrentCellDirtyStateChanged
If TypeOf dgvOptions.CurrentCell.OwningColumn Is DataGridViewCheckBoxColumn AndAlso dgvOptions.IsCurrentCellDirty Then
dgvOptions.CommitEdit(DataGridViewDataErrorContexts.CurrentCellChange)
If DirectCast(dgvOptions.CurrentCell.Value, TriState) = TriState.Common Then
dgvOptions.CurrentCell.Value = TriState.TurnedOff
dgvOptions.EndEdit()
End If
End If
End Sub因此,解决方案是将ThreeState属性的DataGridViewCheckBoxColumn设置为True,并强制DataGridView立即将更改提交到复选框,这样您就可以测试不确定设置,并将Value属性改为未选中。
想了解更多信息,此链接帮了我很多忙。
https://stackoverflow.com/questions/26736008
复制相似问题