PropertyGrid直接绑定Dictionary显示的是数据类型,若要显示为Text|Value需要处理一下。
直接绑定显示如下:
我们希望显示如下:
private void Form6_Load(object sender, EventArgs e)
{
Dictionary<int, string> dicTest = new Dictionary<int, string>();
dicTest.Add(0, "第一项");
dicTest.Add(3, "第二项");
dicTest.Add(5, "第三项");
dicTest.Add(1, "第四项");
//propertyGrid1.SelectedObject = dicTest;
//IDictionary d = new Hashtable();
//d["Hello"] = "World";
//d["Meaning"] = 42;
//d["Shade"] = Color.ForestGreen;
propertyGrid1.SelectedObject = new DictionaryPropertyGridAdapter(dicTest);
}
class DictionaryPropertyGridAdapter : ICustomTypeDescriptor
{
IDictionary _dictionary;
public DictionaryPropertyGridAdapter(IDictionary d)
{
_dictionary = d;
}
//Three of the ICustomTypeDescriptor methods are never called by the property grid, but we'll stub them out properly anyway:
public string GetComponentName()
{
return TypeDescriptor.GetComponentName(this, true);
}
public EventDescriptor GetDefaultEvent()
{
return TypeDescriptor.GetDefaultEvent(this, true);
}
public string GetClassName()
{
return TypeDescriptor.GetClassName(this, true);
}
//Then there's a whole slew of methods that are called by PropertyGrid, but we don't need to do anything interesting in them:
public EventDescriptorCollection GetEvents(Attribute[] attributes)
{
return TypeDescriptor.GetEvents(this, attributes, true);
}
EventDescriptorCollection System.ComponentModel.ICustomTypeDescriptor.GetEvents()
{
return TypeDescriptor.GetEvents(this, true);
}
public TypeConverter GetConverter()
{
return TypeDescriptor.GetConverter(this, true);
}
public object GetPropertyOwner(PropertyDescriptor pd)
{
return _dictionary;
}
public AttributeCollection GetAttributes()
{
return TypeDescriptor.GetAttributes(this, true);
}
public object GetEditor(Type editorBaseType)
{
return TypeDescriptor.GetEditor(this, editorBaseType, true);
}
public PropertyDescriptor GetDefaultProperty()
{
return null;
}
PropertyDescriptorCollection
System.ComponentModel.ICustomTypeDescriptor.GetProperties()
{
return ((ICustomTypeDescriptor)this).GetProperties(new Attribute[0]);
}
//Then the interesting bit. We simply iterate over the IDictionary, creating a property descriptor for each entry:
public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
ArrayList properties = new ArrayList();
foreach (DictionaryEntry e in _dictionary)
{
properties.Add(new DictionaryPropertyDescriptor(_dictionary, e.Key));
}
PropertyDescriptor[] props =
(PropertyDescriptor[])properties.ToArray(typeof(PropertyDescriptor));
return new PropertyDescriptorCollection(props);
}
}
class DictionaryPropertyDescriptor : PropertyDescriptor
{
//PropertyDescriptor provides 3 constructors. We want the one that takes a string and an array of attributes:
IDictionary _dictionary;
object _key;
internal DictionaryPropertyDescriptor(IDictionary d, object key)
: base(key.ToString(), null)
{
_dictionary = d;
_key = key;
}
//The attributes are used by PropertyGrid to organise the properties into categories, to display help text and so on. We don't bother with any of that at the moment, so we simply pass null.
//The first interesting member is the PropertyType property. We just get the object out of the dictionary and ask it:
public override Type PropertyType
{
get { return _dictionary[_key].GetType(); }
}
//If you knew that all of your values were strings, for example, you could just return typeof(string).
//Then we implement SetValue and GetValue:
public override void SetValue(object component, object value)
{
_dictionary[_key] = value;
}
public override object GetValue(object component)
{
return _dictionary[_key];
}
public override bool IsReadOnly
{
get { return false; }
}
public override Type ComponentType
{
get { return null; }
}
public override bool CanResetValue(object component)
{
return false;
}
public override void ResetValue(object component)
{
}
public override bool ShouldSerializeValue(object component)
{
return false;
}
}
private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
{
MessageBox.Show(e.ChangedItem.Label + "," + e.ChangedItem.Value);
}url:http://greatverve.cnblogs.com/archive/2012/02/08/propergrid-Dictionary.html
参考:http://www.differentpla.net/content/2005/02/using-propertygrid-with-dictionaryDictionary<int,string>排序Dictionary无序字典,但是其中int是有大小之分的,某种情况下,需要按int的大小进行排序。
1 Dictionary<int, string> dicTest = new Dictionary<int, string>();
2 dicTest.Add(0, "第一项");
3 dicTest.Add(3, "第二项");
4 dicTest.Add(5, "第三项");
5 dicTest.Add(1, "第四项");
需要对上面的dicTest按key进行排序,.net3.5在LINQ中很好实现,如下代码:
1 var dicResut = from pair in dicTest
2 orderby pair.Key
3 select pair;
但是项目用的.net2.0,没办法,自己排吧,我用的方法是:Dictionary-->DataTalbe排序-->Dictionary
1 Dictionary<int,string> sortDic(Dictionary<int,string> dic,string type)
2 {
3 if (type==string.Empty)
4 {
5 type = "asc";
6 }
7 //转存在datatable
8 DataTable dt = new DataTable();
9 DataColumn id= new DataColumn("id", typeof(int));
10 dt.Columns.Add(id);
11 DataColumn str = new DataColumn("str", typeof(string));
12 dt.Columns.Add(str);
13 DataRow dr;
14 foreach (KeyValuePair<int,string> p in dic)
15 {
16 dr = dt.NewRow();
17 dr["id"] = p.Key;
18 dr["str"] = p.Value;
19 dt.Rows.Add(dr);
20 }
21 //排序
22 DataRow[] rows=dt.Select(string.Empty, " id " + type);
23
24 //排序后的放到Dictionary
25 Dictionary<int, string> newdic = new Dictionary<int, string>();
26 foreach (DataRow drr in rows)
27 {
28 newdic.Add(int.Parse(drr["id"].ToString()), drr["str"].ToString());
29 }
30 dt.Dispose();
31 return newdic;
32
33 }
用SortedDictionary<TKey,TValue>替代Dictionary<TKey,TValue>。
出处: http://www.th7.cn/Program/net/2012/03/26/66438.shtml