During my consultancy engagements I have come across numerous time when you need to sort the value displayed in the drop down box or in the the other Data bound control. It can be very frustrating to do that if you don't have much experience with .Net and you have to repeat similar code again and again. Well .Net 2.0 will ease it quite a bit with Generics ... For now here is a class that works exactly like sorted list but sorts the List by Value instead of Key ...
This is meant for reasonable size list but not very huge lists. There will be performance degradation for real huge lists. I found the code from Code Project or some other site and forgot the name of the author so If you know about him put this in the feedback and I be more then happy to post it in this Post. I made a few changes to it to make it a little more efficient.
Just create a Class library and add the following Class.
Usage ...
using Jawad.Utils.Collection;
..................
LookupCoolection myColl = new LookupCollection();
myColl.Add(“Key1“, “Oracle“)
myCollAdd(“Key2“, “Microsoft“);
myDropDown.DataSource = MyColl;
myDropDown.DataBind();
The Dropdown now will show Microsoft first and Oracle second unlike if we have used Sorted list.
using System;
using System.Collections;
using Cdi.Ces.Utils.Collection.internalUse;
namespace Jawad.Utils.Collection
{
[Serializable]
public class LookupCollection : ICollection, IDictionary, IEnumerable
{
private ArrayList mItems = new ArrayList();
//---------------------------------------------------------------------
// Constructor
//---------------------------------------------------------------------
public LookupCollection()
{
}
//---------------------------------------------------------------------
// Add
//---------------------------------------------------------------------
public void Add(object key, object value)
{
// do some validation
if (key == null)
throw new ArgumentNullException("key is a null reference");
else if (this.Contains(key))
throw new
ArgumentException("An element with the same key already exists");
// add the new item
Lookup newItem = new Lookup();
newItem.Key = key;
newItem.Value = value;
this.mItems.Add(newItem);
}
//---------------------------------------------------------------------
// Clear
//---------------------------------------------------------------------
public void Clear()
{
this.mItems.Clear();
}
//---------------------------------------------------------------------
// Contains
//---------------------------------------------------------------------
public bool Contains(object key)
{
return (this.GetByKey(key) != null);
}
//---------------------------------------------------------------------
// CopyTo
//---------------------------------------------------------------------
public void CopyTo(Array array, int index)
{
this.mItems.CopyTo(array, index);
}
//---------------------------------------------------------------------
// GetEnumerator (1)
//---------------------------------------------------------------------
public IDictionaryEnumerator GetEnumerator()
{
this.mItems.Sort();
return new LookupEnumerator(this.mItems);
}
//---------------------------------------------------------------------
// GetEnumerator (2)
//---------------------------------------------------------------------
IEnumerator IEnumerable.GetEnumerator()
{
this.mItems.Sort();
return new LookupEnumerator(this.mItems);
}
//---------------------------------------------------------------------
// Remove
//---------------------------------------------------------------------
public void Remove(object key)
{
if (key == null)
throw new ArgumentNullException("key is a null reference");
Lookup deleteItem = this.GetByKey(key);
if (deleteItem != null)
{
this.mItems.Remove(deleteItem);
}
}
//=====================================================================
// PRIVATE
//=====================================================================
private Lookup GetByKey(object key)
{
Lookup result = null;
int keyIndex = -1;
ArrayList keys = (ArrayList)this.Keys;
if (this.mItems.Count > 0)
{
keyIndex = keys.IndexOf(key);
if (keyIndex >= 0)
{
result = (Lookup)this.mItems[keyIndex];
}
}
return result;
}
//=====================================================================
// PROPERTIES
//=====================================================================
public int Count
{
get
{
return this.mItems.Count;
}
}
public bool IsSynchronized
{
get
{
return false;
}
}
public object SyncRoot
{
get
{
return this;
}
}
public bool IsFixedSize
{
get
{
return false;
}
}
public bool IsReadOnly
{
get
{
return false;
}
}
public object this[object key]
{
get
{
if (key == null)
throw new ArgumentNullException("key is a null reference");
object result = null;
Lookup findItem = this.GetByKey(key);
if (findItem != null)
{
result = findItem.Value;
}
return result;
}
set
{
}
}
public ICollection Keys
{
get
{
ArrayList result = new ArrayList();
this.mItems.Sort();
foreach (Lookup curItem in this.mItems)
{
result.Add(curItem.Key);
}
return result;
}
}
public ICollection Values
{
get
{
ArrayList result = new ArrayList();
this.mItems.Sort();
foreach (Lookup curItem in this.mItems)
{
result.Add(curItem.Value);
}
return result;
}
}
}
}