Solved

Displaying the number of selected records in a datagrid

Posted on 2004-10-11
6
219 Views
Last Modified: 2010-04-23
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.
0
Comment
Question by:quickly_james
  • 3
  • 3
6 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 12283536
0
 

Author Comment

by:quickly_james
ID: 12283865
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.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 12285332
It seems that the CurrentCellChanged event occurs before the actual IsSelected state has toggled. Try using another event that is triggered later.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:quickly_james
ID: 12285756
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??
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 125 total points
ID: 12286078
You have to use the MouseUp event (the state is not yet toggled into the MouseDown event).
0
 

Author Comment

by:quickly_james
ID: 12294395
Thanks emoreau!
0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question