title: Displaying number of rows checked/unchecked on datagridview
body: <p>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:</p>
  
  <pre><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 &amp; " 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 &amp; " Record(s) Selected"
              Next
          End If
  
      End Sub
  </code></pre>
  
  <p>I placed another procedure when the user selects one record at a time:</p>
  
  <pre><code>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 &amp; " Record(s) Selected"
  
      End If
  Next
  
  End Sub
  </code></pre>
  
  <p>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</p>