Link to home
Start Free TrialLog in
Avatar of quickly_james
quickly_james

asked on

Displaying the number of selected records in a datagrid

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"
    End Sub

    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
            End If
        Next row
        Return iSelectionCount
    End Function

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?

Thanks.
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Avatar of quickly_james
quickly_james

ASKER

Thanks for that link, I did find an error in my code after looking at that and have since updated it to this:

   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"
    End Sub

    Public Function SelectionCount(ByRef Grid As DataGrid) As Integer
        Dim rowCount As Integer = Grid.BindingContext(Grid.DataSource, Grid.DataMember).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
            End If
        Next row
        Return iSelectionCount
    End Function

However, I still find that it runs the MyGrid_CurrentCellChanged event handler before it actually marks the row as being selected. As such the title bar of my DataGrid shows an incorrect number of selected rows.

For example if I select 1 row it says "0 selected". If I then select a second row whilst holding CTRL, thus having 2 rows in the grid high-lighted, its says "1 selected". If I then select a different row, WITHOUT holding CTRL, thus having 1 row high-lighted, its says "2 selected".

The title bar text appears to be always "one step behind" the actual selection count for the grid.
It seems that the CurrentCellChanged event occurs before the actual IsSelected state has toggled. Try using another event that is triggered later.
I have looked through the events for the DataGrid and the only other event I can think of to use is the MouseDown event. However, this has the same effect as the CurrentCellChanged event, it's fired before the IsSelected state has toggled.

Any ideas??
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks emoreau!