Presenter:Beth Massi
Visual Basic programmers will learn how to enable sorting support within their business object collections in order to use the BindingSource to sort them on Windows Forms.
Click here to go to original video page
The following text is a software generated transcript of the video. Click on a minute link to jump to a location within the video
Minute 0
I might Elizabeth Massie and welcome to the Windows forms originated in a series in its next video I m been a show you how to add sorting support to your custom business objects in the last two videos we created some custom classes city and state and set up data binding on Finley added searching supports we can find objects in our collection by specifying search strings now we want to add simple sorting support recollections of that we can apply and remove sorts on the property in order to implement sorting would have to do for steps were going to need to inherit from binding list of tea they were going to override some properties on that finding us they will override apply sort core and removes work for methods to provide the implementation for sorting and removing the sort to do this will need to create a sort compare by implementing I compare so the application open that we ve been working on just a simple form to graze down or accustomed
Minute 1
Objects we have a city class with a CD property is strained we also have a state class with a state property and string and also contains a collection of cities as binding list daily added searchability to our collection by inheriting from binding list and create a searchable binding lest we overrode defined color method here so basically we can pass in the property name and its value and this collection will return the object said now we want to do is let want to augment this search will binding list in order to be able to sort the objects inside so the first thing we re going to do is work going to be named the class so it s a little bit sortable binding last network going to need to override some properties and methods in order to implement the sorting
Minute 2
Now if you just need to support sorting of the collection things would be too complicated is the ability to remove the sort that complicates things because when you sort the collection you need to make it so that any code that uses the collection from that point forward see the collection in its new order that means you ask a change the contents of the collection to be it in a different order so that when any code iterates over the collection on its season sorted order so because you have to support removing the sort you also need a way to get the contents of the collection back to their original order support what would any deal is I m going to make it easy on us and just a drag over the properties that we actually need to implement here so I ve got stored it just indicates whether or not he saw the collection is ordered or not we ve got the sort direction the sort property and the original collection this is so that we can remove the sort we need to now then we need to just override a few properties
Minute 3
And one of done it here is the returning trio from support sorting core which indicates to the binding source that were supporting sorting now to make easy on us were not going to allow editing if the collection is sorted now if you need to do that you re going to need to be able to intercept the insert item remove item in clear items and be sure that the same changes can apply to the unsorted collection is original collection as well as the sorted ones but to make things simple for this example were not allow any changes if the sort is applied now in order to sort the objects inside the collection we need what s called a sort compare the tank so it s going to be something that implements I compare so I m going to add a new class Cambridge is in a college simple sort compare
Minute 4
The paint Andalucia is going to just imports our system component model tank and this simple sort compare is going to be and generics and we re going to be able to pass in Oregon business objects of this entity generic sort compare now we decide to make sure we implement I compare of tea okay so now I m going to just drag over the implementation of the compare so we can talk about what it s doing you can t sell with a couple of private fields here the property descriptor which is the property where sorting on this is a simple sort compare us with only going to be able to sort on a single property and in the direction is a descending or descending saw
Minute 5
In doing a sitcom implementing the compare method of the I compare here and it s just going to get the values of that property and it s going to compare those values depending on the direction it will return the correct order okay so that s not going to need to actually get the sorting of our objects to work till I m just going to say that Annenberg and head back over to our searchable sortable binding list now we can actually makes an implementation on the sorting so going to drag in the apply sort cooler method now supply store core is the over of writable method where we are going to create each are symbols or compare images created and we re going to apply the sort on our collection now where did you first know and apply sort internal is deceived the original order in our original collection and tank and then we re going to get the latest
Minute 6
Itself to just use our sort compare and sort it okay so sort is just a method on our list so then we need to just read the ominous change event so that our controls will refresh themselves if they also see that we can remove the sorts of all this does is clear the current connection current collection and then we add all of the objects in a bear original order from the original collection and it clears that original collection of paint so the final piece to fit for this whole thing is all organ and now is specify that we cannot come that we cannot and it s deep values of our objects hang inside of the collection if this sort is on okay so I m just going to bomb shadow couple properties on the allow new and the allowing move on the high binding list which will automatically tell the binding sources that the you cannot add or remove any antics from the list
Minute 7
Palm if the sorts is implemented okay so now to go back to our form stamina view the code here and already now we ve got a searchable sortable binding list in on the deal is when we start up the farm and going to see me die states binding source sort equals state we can even do descending tank to appear and then I m going to also sort need not cities by any source sort carrying out order to get this to work the city s collection is going to also need to be a searchable sortable binding last Sophie go back to our states will see that we only have it specified as a binding lesson I wanted to be a searchable sortable binding less tank of city
Minute 8
Fidelity able to search and sort on our cities is well okay so that s go and run this and you can see now that I ve reversed assorted Washington New York California is in reverse sort order and each city inside is sorted in ascending order today and we can still use are searching capability win the collection is sorted so that s all there is to it still what I wanted to do what is I want to go back to the foreign and I wanted have a button on here that appliance and removes the sort cell lets just double click on this and so we can say will be docked state finding stories sort
Minute 9
Equal to state and need not city is running sores sort equal to OS 2 city descending care told a switch around 10 and omelettes but when Lebanon air to show how we can remove our sort of a second fun all you do all you did it remove a sort is just set to nothing or to an empty string excuse me and cities in case announced from this wartime came because it is sorted in reverse so if I click this you can see now it s sorted in its ascending order pay and then we can remove the sort now it s back in the original order of
Minute 10
And that s all there is to it to implementing sorting on your collection of business objects so I d like to thank you for your time if you like to check out the Visual Basic developer Center will be posting updates these videos there please also check out the Visual Basic team blog and my blog for more information basic
Tuesday, May 27, 2008
Video: How Do I: Add Sorting Support with Object Binding?
Labels:
object binding,
sorting support
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment