Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Displaying the number of selected records in a datagrid

Posted on 2004-10-11
6
Medium Priority
?
225 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 375 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

783 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