Solved

Displaying the number of selected records in a datagrid

Posted on 2004-10-11
6
222 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to make search between pages 17 57
ModalPopup  question 22 54
Filtering a datagrid view 8 42
Need to Interpret Json in Vb.net ( .net 3.5  framework) 11 45
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

739 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