我如何检查vb.net的datagridview列中的复选框是否已选中 [英] how can i check if checkbox is checked in datagridview column in vb.net

查看:378
本文介绍了我如何检查vb.net的datagridview列中的复选框是否已选中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在创建一个计费系统,但在检查datagridview中是否已选中复选框时遇到了麻烦.

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

我的datagridview当前包含以下列:

0:产品代码

1:说明

2:大小

3:费用

4:数量

5:返回?

(Datagrid未绑定任何内容)

(Datagrid not bound to anything)

返回?"列是复选框列.这样一来,如果用户要退回商品,则他们可以选中要退回的每个商品的复选框,然后根据复选框是否选中来执行不同的代码集.

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.

例如: 如果用户退回价值20英镑的商品并购买了50英镑的商品,则系统应向用户提供50英镑的总费用 但是,如果用户同时购买这两种商品,则系统应输出£70.

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.

这将完全取决于天气情况,或者是否选中了返回"复选框.

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

执行此计算的代码我没有问题,我已经编写了它.但是,它是检查天气的代码,还是未在指定的datagridview列中选中任何复选框.

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.

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

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.

我希望我已经弄清楚了我的情况和问题,希望有人可以提供帮助,谢谢.

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

推荐答案

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

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

''' <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() {"Jack", "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

这是一种语言扩展方法,该方法也很有帮助,只需按一下按钮,您就可以获取所有已检查的行.调整起来很容易,这样您就可以要求选中或未选中的行.

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

用法

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

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

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

这篇关于我如何检查vb.net的datagridview列中的复选框是否已选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆