我目前正在创建一个计费系统,但无法检查内是否选中了一个复选框datagridview
。
我的datagridview
当前包含以下几列:
0:产品编号
1:说明
2:大小
3:费用
4:数量
5:回来吗?
(Datagrid不受任何约束)
回报?” 列是复选框列。这样一来,如果用户要退回商品,则他们可以选中要退回的每个商品的复选框,然后根据复选框是否选中来执行不同的代码集。
例如:如果用户退回一件价值20英镑的物品并购买一件价值50英镑的物品,则系统应向用户显示50英镑的总费用。但是,如果用户同时购买这两种物品,则系统应输出英镑。 70
这将完全取决于天气,或者是否选中了“返回”复选框。
进行此计算的代码我没有问题,我已经编写了它。但是,它是检查天气的代码,还是未在指定的datagridview列中选中任何复选框。
我以为它类似于将用于普通复选框的代码,If Checkbox1.CheckState = CheckState.Checked then ...
但事实并非如此。
我希望我已经清楚说明了我的情况和问题,希望有人可以提供帮助,谢谢。
这是一个完整的示例,其中在IDE中创建了DataGridViewColumns,因此没有代码显示正在创建它们。
''' <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
这是一种语言扩展方法,它也很有用,因为只需按一下按钮,您就可以获取所有选中的行。调整起来很容易,这样您就可以要求选中或未选中的行。
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中完成的,如果使用较低版本,您仍然可以在线查看代码。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句