Displaying the number of selected records in a datagrid
Posted on 2004-10-11
I have a winforms datagrid in which I want to display the number of selected rows in the title bar. So far I have written the following code to do this for me.
Private Sub MyGrid_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyGrid.CurrentCellChanged
Dim iCount As Integer = SelectionCount(MyGrid)
Me.MyGrid.CaptionText = iCount.ToString + " selected"
Public Function SelectionCount(ByRef Grid As DataGrid) As Integer
Dim cm As CurrencyManager = CType(Grid.BindingContext(Grid.DataSource), CurrencyManager)
Dim rowCount As Integer = cm.Count
Dim iSelectionCount As Integer = 0
Dim row As Integer
Dim DRV As DataRowView
For row = 0 To rowCount - 1
If Grid.IsSelected(row) Then
iSelectionCount += 1
It seems to display the selection count for the previously selected numbers. That is, it runs the currentcellchanged handler before it actually marks the row as being selected.
Does anyone know how I can get around this?