Jose M.
Jose M.

Reputation: 2340

Displaying number of rows checked/unchecked on datagridview

I have a datagrid on a form with a checkbox to select all records. Column (0) of my datagrid is a checkbox column, and on the header I put a checkbox to select all records and display the number of records selected. Here is my code:

Private Sub chkSelectRecords_CheckedChanged(sender As Object, e As EventArgs) Handles chkSelectRecords.CheckedChanged

        'Procedure runs when the checkSelectRecords is clicked.
        'The loop selects all records on the datagrid when checked
        'and clears selection when unchecked.

        Dim chkRow As Integer = 0

        If chkSelectRecords.Checked = True Then

            For Each row As DataGridViewRow In grdDeleteRecord.Rows

                row.Cells(0).Value = (row.Cells(0).Value IsNot DBNull.Value)
                chkRow += 1

                lblRowCount.Visible = True
                lblRowCount.Text = chkRow.ToString & " Record(s) Selected"
           Next
        Else
            For Each row As DataGridViewRow In grdDeleteRecord.Rows

                row.Cells(0).Value = (row.Cells(0).Value Is DBNull.Value)
                chkRow += 0

                lblRowCount.Text = chkRow.ToString & " Record(s) Selected"
            Next
        End If

    End Sub

I placed another procedure when the user selects one record at a time:

Private Sub grdDeleteRecord_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles grdDeleteRecord.CellContentClick

' Shows how to get count of checked or unchecked checkbox column count along with
' how to get column data back for checked items.

Dim chkRowCount As Integer = 0

grdDeleteRecord.EndEdit()

For Each row As DataGridViewRow In grdDeleteRecord.Rows

    If row.Cells(0).Value = True Then
        chkRowCount += 1
    Else
        chkRowCount += 0

        lblRowCount.Visible = True
        lblRowCount.Text = chkRowCount.ToString & " Record(s) Selected"

    End If
Next

End Sub

Both procedures work as needed. The problem is when I select all records my label does not update the number of records that are now selected. I am sure this is happening because they are two separate events, I just don't know how to link the events or write a procedure that accomplishes all three tasks

Upvotes: 0

Views: 1310

Answers (1)

Trevor
Trevor

Reputation: 8004

Here's a function I wrote for you. You must use the CellValueChangedEvent and the CurrentCellDirtyStateChanged events...

 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    lblRowCount.Text = SetLabel()
 End Sub


Private Function SetLabel() As String
    Dim strRows As String = String.Empty
    Dim intRows As Integer = 0

    For i As Integer = 0 To grdDeleteRecord.Rows.Count - 1
       If CBool(grdDeleteRecord(0, i).Value) Then
          intRows += 1
       End If
    Next

    If intRows > 0 Then
        strRows = intRows.ToString & " Record(s) Selected"
        lblRowCount.Visible = True
    End If

    Return strRows
End Function

Private Sub grdDeleteRecord_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles grdDeleteRecord.CellValueChanged
    lblRowCount.Text = SetLabel()
End Sub

Private Sub grdDeleteRecord_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles grdDeleteRecord.CurrentCellDirtyStateChanged
    If grdDeleteRecord.IsCurrentCellDirty Then
        grdDeleteRecord.CommitEdit(DataGridViewDataErrorContexts.Commit)
    End If
End Sub

When you check or uncheck these checkboxes it will trigger this event and thus update your label. Then you can use this where ever you need it to be used.

Upvotes: 1

Related Questions