Solved

DataGridView Binding Source

Posted on 2014-03-19
9
370 Views
Last Modified: 2014-03-20
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
Comment
Question by:Alyanto
  • 4
  • 4
9 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39939409
You have to apply the sort to the bindingsource instead, rather than to the GridView itself:
YourBindingSource.Sort = "ColumnToSortOn"

Open in new window

0
 
LVL 1

Author Comment

by:Alyanto
ID: 39939525
If I understand you correctly I will need to change the scope of the bindingsource from procedure to class for this to work?
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39939540
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
 
LVL 1

Author Comment

by:Alyanto
ID: 39939563
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39939609
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
 
LVL 1

Author Comment

by:Alyanto
ID: 39939740
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39939847
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
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 39940049
If you use an in memory datatable instead of collections, sorting should work just fine.
0
 
LVL 1

Author Closing Comment

by:Alyanto
ID: 39942945
Spot on answer, thank you very much
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

932 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

12 Experts available now in Live!

Get 1:1 Help Now