how can i check if checkbox is checked in datagrid

2020-04-19 08:33发布

问题:

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

My datagridview currently contains the following columns:

0 : Product Code

1 : Description

2 : Size

3 : Cost

4 : Quantity

5 : Return?

(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.

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.

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.

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.

回答1:

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

Usage

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

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.