databinding to a dictionary

D

Dan Holmes

I have a custom dictionary and i would like to bind it to a control.
Since Dictionary doesn't implement IList i would have to write it. Is
the only way to do this to keep another collection that matches the key
in the dictionary to an index in the collection?

dan

Here is the code i want to add IList to.

public interface ISystemMessageData
{
String MessageID { get; set;}
String WhseID { get; set;}
String Flag { get; set;}
String MessageDate { get; set;}
String UserID { get; set;}
String Message { get; set;}
String PersonID { get; set;}
String MachineName { get; set;}
String DeviceID { get; set;}
String Subject { get; set;}
String ToEmail { get; set;}
String EmailFlag { get; set;}
String FromEmail { get; set;}
}
[Serializable]
public class RemoteDataCollection<TKey, TValue> :
System.Collections.Generic.Dictionary<TKey, TValue>
{
public RemoteDataCollection() { }
protected
RemoteDataCollection(System.Runtime.Serialization.SerializationInfo
info, System.Runtime.Serialization.StreamingContext context) :
base(info, context) { }
}

[Serializable]
public class SystemMessageDataCollection :
RemoteDataCollection<String, ISystemMessageData>
{
public SystemMessageDataCollection() { }
protected
SystemMessageDataCollection(System.Runtime.Serialization.SerializationInfo
info, System.Runtime.Serialization.StreamingContext context):base(info,
context){}
}
 
D

Dan Holmes

Here is what i did and i must have done something wrong cause it doesn't
work. What did i do wrong?

[Serializable]
public class RemoteDataCollection<TKey, TValue> :
System.Collections.Generic.Dictionary<TKey, TValue>, IList<TValue>
{
protected System.Collections.Generic.List<TValue> _list = null;
public RemoteDataCollection()
{
_list = new List<TValue>();
}
protected
RemoteDataCollection(System.Runtime.Serialization.SerializationInfo
info, System.Runtime.Serialization.StreamingContext context) :
base(info, context) { }

public virtual void Add(TKey key, TValue value)
{
base.Add(key, value);
_list.Add(value);
}
public virtual bool Remove(TKey key)
{
TValue value;
bool b = this.TryGetValue(key, out value);
_list.Remove(value);
base.Remove(key);
return b;
}

#region IList<TValue> Members

public int IndexOf(TValue item)
{
return _list.IndexOf(item);
}

public void Insert(int index, TValue item)
{
throw new Exception("The method or operation is not
implemented: RemoveAt.");
}

public void RemoveAt(int index)
{
throw new Exception("The method or operation is not
implemented: RemoveAt.");
}

public TValue this[int index]
{
get
{
throw new Exception("The method or operation is not
implemented.");
}
set
{
throw new Exception("The method or operation is not
implemented.");
}
}

#endregion

#region ICollection<TValue> Members

public void Add(TValue item)
{

((System.Collections.Generic.ICollection<TValue>)base.Values).Add(item);
}

public bool Contains(TValue item)
{
return
((System.Collections.Generic.ICollection<TValue>)base.Values).Contains(item);
}

public void CopyTo(TValue[] array, int arrayIndex)
{

((System.Collections.Generic.ICollection<TValue>)base.Values).CopyTo(array,
arrayIndex);
}

public bool IsReadOnly
{
get { return
((System.Collections.Generic.ICollection<TValue>)base.Values).IsReadOnly; }
}

public bool Remove(TValue item)
{

((System.Collections.Generic.ICollection<TValue>)base.Values).Remove(item) ;
return true;
}

#endregion

#region IEnumerable<TValue> Members

public new IEnumerator<TValue> GetEnumerator()
{
return this.GetEnumerator();
}

#endregion
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top