我如何检查是否在 vb.net 的 datagridview 列中选中了复选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36230432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:46:44  来源:igfitidea点击:

how can i check if checkbox is checked in datagridview column in vb.net

vb.netwinformsvisual-studiocheckboxdatagridview

提问by FMG97

I am currently creating a billing system and I am having trouble with checking if a checkbox is checked within a datagridview.

我目前正在创建一个计费系统,但在检查datagridview.

My datagridviewcurrently contains the following columns:

datagridview目前包含以下列:

0 : Product Code

0 : 产品代码

1 : Description

1:说明

2 : Size

2:尺寸

3 : Cost

3:成本

4 : Quantity

4:数量

5 : Return?

5:回来?

(Datagrid not bound to anything)

(Datagrid 没有绑定到任何东西)

The "Return?" column is the checkbox column. This is so that if the user is returning items, then they can check the checkboxes for each item they are returning which will then carry out a different set of code depending on if the checkboxes are checked or not.

回报?” column 是复选框列。这样,如果用户正在返回项目,那么他们可以检查他们正在返回的每个项目的复选框,然后根据是否选中复选框执行一组不同的代码。

For Example: If the user is returning an item costing £20 and purchasing an item that costs £50 then the system should present the user with a total cost of £50 However, if the user is purchasing both items then the system should output £70.

例如:如果用户退回价值 £20 的物品并购买价值 £50 的物品,则系统应向用户显示 £50 的总成本 但是,如果用户同时购买这两种物品,则系统应输出 £ 70.

This will all depend on weather or not the return checkbox is checked.

这将完全取决于天气或是否选中返回复选框。

The code that carries out this calculation i have no problem with, i have already written it. However, it is the code that checks weather or not any of the checkboxes are checked within the specified datagridview column.

执行这个计算的代码我没有问题,我已经写好了。但是,它是检查天气或在指定的 datagridview 列中没有选中任何复选框的代码。

I assumed it was similar to the code that would be used for a normal checkbox If Checkbox1.CheckState = CheckState.Checked then ...but it is not.

我认为它类似于用于普通复选框的代码,If Checkbox1.CheckState = CheckState.Checked then ...但事实并非如此。

I hope I have made my scenario and problem clear to understand and that someone can help, thanks.

我希望我已经清楚地理解了我的场景和问题,并且有人可以提供帮助,谢谢。

回答by Karen Payne

here is a complete example where the DataGridViewColumns are created in the IDE so there is no code showing them being created.

这是一个完整的示例,其中 DataGridViewColumns 在 IDE 中创建,因此没有显示它们正在创建的代码。

''' <summary>
''' DataGridView columns were created in the IDE
''' </summary>
''' <remarks></remarks>
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        DataGridView1.Rows.Add(New Object() {"John", "USA", True})
        DataGridView1.Rows.Add(New Object() {"Mike", "AU", False})
        DataGridView1.Rows.Add(New Object() {"Hyman", "EU", True})
        DataGridView1.Rows.Add(New Object() {"Mike", "AU", False})
    End Sub
    Private Sub DataGridView1SelectAll_CurrentCellDirtyStateChanged(
        ByVal sender As Object,
        ByVal e As EventArgs) Handles DataGridView1.CurrentCellDirtyStateChanged

        RemoveHandler DataGridView1.CurrentCellDirtyStateChanged,
            AddressOf DataGridView1SelectAll_CurrentCellDirtyStateChanged

        If TypeOf DataGridView1.CurrentCell Is DataGridViewCheckBoxCell Then
            DataGridView1.EndEdit()
            Dim Checked As Boolean = CType(DataGridView1.CurrentCell.Value, Boolean)
            If Checked Then
                MessageBox.Show("You have checked")
            Else
                MessageBox.Show("You have un-checked")
            End If
        End If

        AddHandler DataGridView1.CurrentCellDirtyStateChanged,
            AddressOf DataGridView1SelectAll_CurrentCellDirtyStateChanged
    End Sub
End Class

enter image description here

在此处输入图片说明

Here is a language extension method that also be helpful in that by say pressing a button you can get all rows that are checked. It would be easy to adjust so that you could ask for either checked or unchecked rows.

这是一种语言扩展方法,它也很有帮助,通过说按下按钮,您可以获得所有被检查的行。调整很容易,以便您可以要求选中或未选中的行。

Module Module1
    <System.Diagnostics.DebuggerStepThrough()> _
    <Runtime.CompilerServices.Extension()> _
    Public Function GetCheckedRows1(
        ByVal GridView As DataGridView,
        ByVal ColumnName As String) As List(Of DataGridViewRow)
        Return _
            (
                From SubRows In
                    (
                        From Rows In GridView.Rows.Cast(Of DataGridViewRow)()
                        Where Not Rows.IsNewRow
                    ).ToList
                Where CBool(SubRows.Cells(ColumnName).Value) = True
            ).ToList
    End Function
End Module

Usage

用法

Dim rowsCheckedList As List(Of DataGridViewRow) =
    DataGridView1.GetCheckedRows1("ProcessColumn")

See also my MSDN code sampleson this topic. They are done in VS2013 and if using a lesser version you can still view the code online.

另请参阅我关于此主题的MSDN 代码示例。它们是在 VS2013 中完成的,如果使用较小的版本,您仍然可以在线查看代码。