【问题标题】:Issue detecting checkbox state in DataGridView在 DataGridView 中检测复选框状态的问题
【发布时间】:2011-06-20 18:52:57
【问题描述】:

我在一个包含复选框列的 .Net 应用程序中有一个 DataGridView 控件。我希望用户能够编辑复选框。我遇到的问题是用户检查后无法检测到复选框的状态。

如果复选框最初被选中,那么一旦 DataGridViewCheckBoxCell 获得焦点,它将返回选中状态。但是,如果我再次单击该复选框并取消选中它,那么它仍然会返回选中状态。从那时起,无论复选框的实际状态如何,它都将始终返回选中状态,直到它失去焦点并再次获得它。

同样,如果复选框最初是未选中的,那么当它获得焦点时,无论复选框的实际状态是什么,它都会在点击事件中返回未选中状态。

这是我的代码。

    Private Sub grdTemplates_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdTemplates.CellContentClick
    Dim strValue As String = ""
    Try
        If Me.grdTemplates.Columns(e.ColumnIndex).Name = "colCurrentTemplate" Then
            'The user clicked on the checkbox column
            strValue = Me.grdTemplates.Item(e.ColumnIndex, e.RowIndex).Value

            'THIS VALUE NEVER CHANGES WHILE THE DataGridViewCheckBoxCell HAS FOCUS
            Me.lblTemplates.Text = strValue
        End If

    Catch ex As Exception
        HandleError(ex.ToString)
    End Try

End Sub

提前致谢,

迈克

【问题讨论】:

    标签: vb.net datagridview datagridviewcheckboxcell


    【解决方案1】:

    将其包含在您的代码中:

    Sub dataGridView1_CurrentCellDirtyStateChanged(ByVal sender As Object, ByVal e As EventArgs) Handles dataGridView1.CurrentCellDirtyStateChanged
        If dataGridView1.IsCurrentCellDirty Then
            dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
        End If
    End Sub
    

    来源:http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.currentcelldirtystatechanged.aspx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-28
      • 1970-01-01
      • 2011-02-19
      • 1970-01-01
      • 2012-01-28
      • 1970-01-01
      • 2011-08-02
      相关资源
      最近更新 更多