사전을 DataGridView
또는 PropertyGrid
에서 편집하려면 ICustomTypeDescriptor
을 구현해야합니다.
옵션 1 - 구현은 ICustomTypeDescriptor 당신은 ICustomTypeDescriptor
을 구현 한 다음 DataGridView
에서 사전을 편집 할 수 있습니다
. 약간의 변경으로 this 구현을 사용할 수 있습니다.
Dictionary<string, int> dictionary;
public void Form1_Load(object sender, EventArgs e)
{
dictionary = new Dictionary<string, int>() { { "A", 1 }, { "B", 2 }, { "C", 3 } };
dataGridView1.DataSource = new BindingSource(new DictionaryAdapter(dictionary) , "");
}
또는 당신이 선호하는 경우에, 당신은 PropertyGrid
의 SelectedObject
로 설정할 수 있습니다 :
public class DictionaryAdapter : ICustomTypeDescriptor
{
IDictionary dictionary;
public DictionaryAdapter(IDictionary d)
{
dictionary = d;
}
public string GetComponentName()
{
return TypeDescriptor.GetComponentName(this, true);
}
public EventDescriptor GetDefaultEvent()
{
return TypeDescriptor.GetDefaultEvent(this, true);
}
public string GetClassName()
{
return TypeDescriptor.GetClassName(this, true);
}
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[] { });
}
public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
ArrayList properties = new ArrayList();
foreach (DictionaryEntry e in dictionary)
{
properties.Add(new DictionaryPropertyDescriptor(dictionary,
e.Key.ToString()));
}
PropertyDescriptor[] props =
(PropertyDescriptor[])properties.ToArray(typeof(PropertyDescriptor));
return new PropertyDescriptorCollection(props);
}
}
public class DictionaryPropertyDescriptor : PropertyDescriptor
{
IDictionary dictionary;
string key;
internal DictionaryPropertyDescriptor(IDictionary d, string k)
: base(k.ToString(), null)
{
dictionary = d;
key = k;
}
public override Type PropertyType
{
get { return dictionary[key].GetType(); }
}
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;
}
}
: 여기
propertyGrid1.SelectedObject = new DictionaryAdapter(dictionary);
이 구현되어 그럼 당신은 단순히 이런 식으로 사전 편집 할 수 있습니다
옵션 2 - 데이터 테이블 사용
또한 간단한 옵션으로 Dictionary
을 DataTable
으로 지정하고 데이터를 편집 할 수 있습니다.
public static class DictionaryExtensions
{
public static DataTable ToDataTable<T>(this Dictionary<string, T> dictionary)
{
var dt = new DataTable();
dictionary.Keys.ToList().ForEach(x => dt.Columns.Add(x, typeof(T)));
dt.Rows.Add(dictionary.Values.Cast<object>().ToArray());
return dt;
}
public static void UpdateFromDataTable<T>(this Dictionary<string, T> dictionary,
DataTable table)
{
if (table.Rows.Count == 1)
table.Columns.Cast<DataColumn>().ToList().ForEach(x =>
dictionary[x.ColumnName] = table.Rows[0].Field<T>(x.ColumnName));
}
}
을 그리고이 확장 방법이 방법 사용 :
이 작업에 대한 확장 메서드를 만들 수 있습니다
Dictionary<string, int> dictionary;
public void Form1_Load(object sender, EventArgs e)
{
dictionary = new Dictionary<string, int>() { { "A", 1 }, { "B", 2 }, { "C", 3 } };
dataGridView1.DataSource = dictionary.ToDataTable();
}
private void button1_Click(object sender, EventArgs e)
{
dictionary.UpdateFromDataTable(dataGridView1.DataSource as DataTable);
}
는 또한 [이 게시물] (HTTP를 살펴 : // 유래합니다. com/a/42611427/3110834). –