Solved

Displaying the number of selected records in a datagrid

Posted on 2004-10-11
6
217 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 69

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 69

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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 69

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now