简体   繁体   English

如何在datagridview复选框上放置一个值?

[英]How to put a value on datagridview checkbox?

My question is about.I have a datagridview in which i have fourth column as checkbox. 我的问题是关于。我有一个datagridview,其中我有第四列作为复选框。

When clicking checkbox i want to get checkbox value in code behind. 当单击复选框时,我想获取后面代码中的复选框值。 Can anybody help? 有人可以帮忙吗?

If you want the results when the checkbox value changes immediately, you'll need to implement a two step process. 如果要在复选框值立即更改时得到结果,则需要执行两步过程。

First you'll need to generate the DataGridView's CurrentCellDirtyStateChange event, Check if the DataGridView's CurrentCell is both not Nothing and if the Index is 3 (since it is your fourth column), and finally commit the DataGridView. 首先,您需要生成DataGridView的CurrentCellDirtyStateChange事件,检查DataGridView的CurrentCell是否都不为Nothing且索引是否为3(因为这是您的第四列),最后提交DataGridView。

Next you'll need to generate the DataGridView's CellContentClick event. 接下来,您将需要生成DataGridView的CellContentClick事件。 You'd need to check if the ColumnIndex is 3 and if so then get the cell's value. 您需要检查ColumnIndex是否为3,如果是,则获取单元格的值。

Here is an example: 这是一个例子:

Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
    If e.RowIndex <> -1 AndAlso e.ColumnIndex = 3 Then
        MessageBox.Show(DataGridView1.Rows.Item(e.RowIndex).Cells(e.ColumnIndex).Value.ToString)
    End If
End Sub

Private Sub DataGridView1_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles DataGridView1.CurrentCellDirtyStateChanged
    If DataGridView1.CurrentCell IsNot Nothing AndAlso DataGridView1.CurrentCell.ColumnIndex = 3 Then
        DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
    End If
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM