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

Sol 3

Home of Barrows Software Solutions, LLC

Keith Barrows - StarPilot

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: , ,

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
New Comments to this post are disabled

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.

This Blog

Syndication

News

Disclaimer
About me

Locations of visitors to this page
weblogs.asp.net/kbarrows
BlogMailr Enabled <script type='text/javascript' src='http://track3.mybloglog.com/js/jsserv.php?mblID=2008010310421330'&gt;&lt;/script> <script type="text/javascript" src="http://pub.mybloglog.com/comm2.php?mblID=2008010310421330&amp;c_width=180&amp;c_sn_opt=y&amp;c_rows=5&amp;c_img_size=f&amp;c_heading_text=Recent+Readers&amp;c_color_heading_bg=005A94&amp;c_color_heading=ffffff&amp;c_color_link_bg=E3E3E3&amp;c_color_link=005A94&amp;c_color_bottom_bg=005A94"></script>
CS Build: 2.1.61129.2
1999
Listed on the CS Listings Powered By Community Server Themed by nb development