Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 410
  • Last Modified:

DataGridView Binding Source

When I try to use the sort function of the DataGridView with a BindingSource it does not work and throws and error that indicates that Sort does not work with a BindingSource.  What is the best approach to sorting.  On the Grid there is an added column that data is enter into it.  This data must persist between sorts.

I have considered dumping the data from the grid into a new object collection apply some Linq and then rebind the grid.  Is this the best way to do this or is there an alternative?
0
Alyanto
Asked:
Alyanto
  • 4
  • 4
1 Solution
 
Carl TawnSystems and Integration DeveloperCommented:
You have to apply the sort to the bindingsource instead, rather than to the GridView itself:
YourBindingSource.Sort = "ColumnToSortOn"

Open in new window

0
 
AlyantoAuthor Commented:
If I understand you correctly I will need to change the scope of the bindingsource from procedure to class for this to work?
0
 
Carl TawnSystems and Integration DeveloperCommented:
That would depend on how/when you create and populate it. But if you are in a WinForms environment and loading it at Form load time, then yes that would be the sensible approach.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

 
AlyantoAuthor Commented:
is the binding source then reapplied to the grids datasource?  
Also what happens to data in columns that were not included in the binding source?
0
 
Carl TawnSystems and Integration DeveloperCommented:
No, you don't need to reapply the data source. Changing the Sort property will trigger an automatic refresh.

For example:
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim cn As New SqlConnection("Server=(local);Initial Catalog=CarlDemo;Integrated Security=SSPI;")
        Dim adap As New SqlDataAdapter("SELECT * FROM TestTable", cn)

        Dim ds As New DataSet
        adap.Fill(ds)

        source.DataSource = ds.Tables(0)
        DataGridView1.DataSource = source

    End Sub

    Private Sub DataGridView1_ColumnHeaderMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.ColumnHeaderMouseClick

        Dim sort As String = DataGridView1.Columns(e.ColumnIndex).Name
        source.Sort = sort

    End Sub

    Private source As New BindingSource()

Open in new window


How are you defining your additional columns?
0
 
AlyantoAuthor Commented:
The bindingsource I have is loaded like the
                    
                  Dim Scans = From s As ScannerFile In ScannerImp.Scanner _
                                       Where s.LongCode = longCode _
                                       Select s

                    Dim DbSource as New BindingSource
                    Dim Zones As New Generic.List(Of ScannerFile.Zone)
                    Zones.AddRange(Scans.First.Zones)  'Zones is a collection of class Zone
                    DbSource.DataSource = Zones
                    dgvScans.DataSource = DbSource

Open in new window


Is using a none database source going to be a problem.  After trying what you have suggested the list does not change order.

NB I have moved the  Dim DbSource as New BindingSource into a form level declaration as private.
0
 
Carl TawnSystems and Integration DeveloperCommented:
Using a non-database source makes things more complicated as you have to get into creating a collection derived from BindingList<> that implements its own comparison and sorting methods.
0
 
CodeCruiserCommented:
If you use an in memory datatable instead of collections, sorting should work just fine.
0
 
AlyantoAuthor Commented:
Spot on answer, thank you very much
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now