检测 DataGridView 中的复选框状态时出现问题

发布于 2024-10-15 21:25:22 字数 1006 浏览 4 评论 0原文

我在 .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

预先感谢,

迈克

I have a DataGridView control in a .Net application that contains a checkbox column. I would like for the user to be able to edit the checkboxes. The issue that I am running into is that I cannot detect the state of the checkbox after the user checks it.

If the checkbox was originally checked, then it will return checked as soon as the DataGridViewCheckBoxCell gets focus. But, if I click on the checkbox again and unchecks it, then it still returns checked. From that point on, it will always return checked regardless of the actual state of the checkbox until it looses focus and gains it again.

Likewise, if the checkbox was originally unchecked, then when it gets focus it will return unchecked in the click event regardless of what the state of the checkbox actually is.

Here is my code.

    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

Thanks in advance,

Mike

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷血 2024-10-22 21:25:22

将其包含在您的代码中:

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

Include this in your code:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文