Welcome to Sol 3 Sign in | Join | Help
CS Search | Live Search Search

Sol 3

Home of Barrows Software Solutions, LLC

Keith Barrows

Subjects range from Personal to Technical.

Using the SortedDictionary<K,V> to move items from one list control to another

An app I am currently working on uses the paradigm of moving items from one listbox to another instead of doing a DB call for each user interaction.

I needed a way to nicely move stuff from left to right and right to left and keep it sorted.  By using Generics I found a quick way to sort the data and rebind it to the control.  What I ended up with looks something like this:

    protected void MoveEntryFromSourceToTarget(ListBox source, ListBox target, string itemText, string itemID)

    {

        DeleteEntryInList(source, itemText);

        AddEntryToList(target, itemText, itemID);

    }

    protected void DeleteEntryInList(ListBox target, string targetText)

    {

        ListItem myItem = target.Items.FindByText(targetText);

        target.Items.Remove(myItem);

    }

    protected void AddEntryToList(ListBox target, string itemText, string itemID)

    {

        ListItem myNewItem = new ListItem(itemText, itemID);

        target.Items.Add(myNewItem);

 

        SortedDictionary<string, string> myList = new SortedDictionary<string, string>();

        foreach (ListItem myItem in target.Items)

        {

            // since this sorts on key and not value, put the text in the key spot...

            myList.Add(myItem.Text, myItem.Value);

        }

        target.Items.Clear();

        foreach (KeyValuePair<string, string> kvp in myList)

        {

            // Remember that the key/value is really value/key...

            target.Items.Add(new ListItem(kvp.Key, kvp.Value));

        }

    }

 

Published Thursday, September 20, 2007 7:23 PM by Keith Barrows
Filed under: , ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Keith Barrows - Aggregated said:

An app I am currently working on uses the paradigm of moving items from one listbox to another instead

September 26, 2007 8:32 PM

Leave a Comment

(required) 
(optional)
(required) 
Submit

About Keith Barrows

I've been in computing since 1975. I started on an old PDP-8J with 3k of memory and 2 teletypes. I learned BASIC and Octal based assembly. I later moved into CPM, TR-DOS, Apple and finally into PC-DOS, Dr DOS and MS-DOS. I've been a beta tester for over a decade, got into web applications as a means to handle B2B requirements and have specialized in data movement between applications and businesses since. I have been a MVP, ASP Elite and was selected by Microsoft as one of the original 15 board members for ASPInsiders.
CS Build: 2.1.61129.2
1999
Listed on the CS Listings Powered By Community Server Themed by nb development