Sorting of a List. Worked at first, not working now.

Greek

New member
Joined
Jul 3, 2014
Messages
3
Programming Experience
10+
In my vb.net (2010) wf project, I had created inside a sub (triggered by a button) a list which I'm using later (inside the same sub) as the datasource of a DataGridView in my form.
To my surprise the DataGridView can't be sorted after its creation (!) so I figured it out (and it worked) that I must sort the members of my list before I assign it as datasource to the grid.
I'm defining the list as follows:

Dim
listNIK = New List(Of Class1Data)

where Class1Data is a separate class with 8 members/columns for each row: Strings, an integer and a date.
Everything worked as expected and the list was fine sorted with:

listNIK.Sort(Function(x, y) x.PARfdate.CompareTo(y.PARfdate))

where PARfdate was the date element in Class1Data

Later I decided to create a sub for deleting a selected row from the list and the datagridview but the Dim listNIK = New List(Of Class1Data) was inside the scope of my button_click sub I mentioned.

So, I took the Dim listNIK... to the beginning of my Public Class code, outside/before any sub and my new "delete selected row" sub worked like a charm!

To my surprise (again) my list/datagridview isn't sorted anymore!!
I'm not getting any errors but the intellisense list doesn't show the .sort anymore! Has only 5 choices (equals, GetType etc.). When I move the Dim listNIK inside the sub again, it gets many choices including .sort

Why is that?
And is there another way to sort my list?
 
You can use any object that implements the IList or IListSource interface as the data source for a DataGridView. That's a lot of different types of objects. In order for the data in the data source to be able to be sorted, the object must also implement the IBindingList interface. It's that interface that provides knowledge of data-binding to the list. The List(Of T) class implements IList but not IBindingList, therefore it doesn't support sorting via a bound control. The BindingList(Of T) class does implement the IBindingList interface, although it doesn't provide a default implementation for sorting. You can inherit BindingList(Of T) and provide your own implementation for sorting your own item type and then use an instance of that derived class as the data source for your grid.

Alternatively, you can put your data into a DataTable. The DataTable class implements the IListSource interface and its GetList method returns a DataView, which implements IList, IBindingList and, for advanced sorting and filtering, IBindingListView. The DataView class does provide a default implementation for sorting so you don't have to. The DataTable/DataView is not strongly-typed to your data though, so you do lose something in order to get that general-purpose sorting.
 
So, I took the Dim listNIK... to the beginning of my Public Class code, outside/before any sub and my new "delete selected row" sub worked like a charm!

To my surprise (again) my list/datagridview isn't sorted anymore!!
I'm not getting any errors but the intellisense list doesn't show the .sort anymore! Has only 5 choices (equals, GetType etc.). When I move the Dim listNIK inside the sub again, it gets many choices including .sort

Why is that?

You'd need to show us the actual code for us to be sure. Tiny snippets and your explanation of what you think you have is not enough. If you're not seeing the Sort method then the reference you're using is not type List(Of T). Most likely you have Option Infer On and Option Strict Off. You should turn Option Strict On now and leave it On for good, except in those very rare cases where you need it Off and, even then, only turn it Off in the specific files that need it. You can turn Option Strict On in the project properties and it applies project-wide. You can also turn it On in the IDE Options and it will be On by default for all future projects.

By having Option Infer On, this code:
Dim listNIK = New List(Of Class1Data)
is inferring the type of the variable from the type of the initialising expression. You are initialising the the variable with an expression of type `List(Of Class1Data)` so that's the type of the variable. Type inference is not supported for member variables though, only local. If you move your declaration out of a method into the class then it becomes a member of that class and type inference is not supported. Option Strict Off allows you to declare a variable without a type and type Object is assumed, therefore that same code at the class level will declare the variable as type Object instead of type `List(Of Class1Data)`. The Object class has no Sort method so you won't see a Sort method. Option Strict On doesn't allow you to declare variables without a type. It wouldn't solve your problem - you'd still have to know to specify the type - but it would prevent the code compiling and alert you to where exactly the issue was rather than letting it run and crashing at run time.

By the way, if you are going to initialise a variable with a New object then don't use `=` to begin with; use `As`. That would have avoided your problem in the first place. This:
Dim listNIK As New List(Of Class1Data)
is actually shorthand for this:
Dim listNIK As List(Of Class1Data) = New List(Of Class1Data)
so it includes the type and the initialising expression. Also, don't use `Dim` for member variables. Specify an access level explicitly. Generally, you're going to want fields to be private so use the Private keyword.
 
I can't believe it! By changing the "=" to "As" my code is working as intended!

My friend jmcilhinney can't thank you enough (I only pressed the little star down left to add to your reputation!)

The project I'm working on is (I think) a very complicated one. It involves taking data from (many) PDF files (not stored as in a database, taken only for one use), manipulate them, post them in dynamic changing webpages/forms (always the same URL but their content changing) with dialog boxes, checkboxes that need to be checked, buttons that need to be presses, elements that appear and disappear in a 2-pane webbrowser and with data interchange between the 2 panes. All that is done with ...one click from the user. A couple of minutes later all the data are submitted properly in the website, a job that a regular user by typing or copy/paste would need hours!

Thanks again. My project is working but is not yet finished.
I feel more calm now that I know, this great community has my back.
 
Back
Top