당신은 GetProperties()
메소드를 오버라이드 (override)하기 위해 CustomTypeDescriptor
의 서브 클래스를 만들려고 할 수 있습니다 내 자신의 목적을 위해,이 같은 DataTable을에 개체를 변환, 다른 방법을 가기로 결정했다. 그런 다음 자신의 TypeDescriptionProvider
클래스를 구현하여 원하는 클래스 (행으로 표시됨)의 TypeDescriptionProviderAttribute
에 설정해야합니다.
예 : 여기 bind second-level properties
내 예이다
MyFieldsClass
[TypeDescriptionProvider(typeof(MyTypeDescriptionProvider))]
internal class MyFieldsClass
{
public int IntField;
public double DoubleField;
}
FieldPropertyDescriptor
internal sealed class FieldPropertyDescriptor<TComponent, TField> : PropertyDescriptor
{
private readonly FieldInfo fieldInfo;
public FieldPropertyDescriptor(string name)
: base(name, null)
{
fieldInfo = typeof(TComponent).GetField(Name);
}
public override bool IsReadOnly { get { return false; } }
public override void ResetValue(object component) { }
public override bool CanResetValue(object component) { return false; }
public override bool ShouldSerializeValue(object component)
{
return true;
}
public override Type ComponentType
{
get { return typeof(TComponent); }
}
public override Type PropertyType
{
get { return typeof(TField); }
}
public override object GetValue(object component)
{
return fieldInfo.GetValue(component);
}
public override void SetValue(object component, object value)
{
fieldInfo.SetValue(component, value);
OnValueChanged(component, EventArgs.Empty);
}
}
MyCustomTypeDe 스크립터
internal sealed class MyCustomTypeDescriptor : CustomTypeDescriptor
{
public MyCustomTypeDescriptor(ICustomTypeDescriptor parent)
: base(parent)
{
}
public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
return GetProperties();
}
public override PropertyDescriptorCollection GetProperties()
{
return AddItems(base.GetProperties(),
new FieldPropertyDescriptor<MyFieldsClass, int>("IntField"),
new FieldPropertyDescriptor<MyFieldsClass, double>("DoubleField"));
}
private static PropertyDescriptorCollection AddItems(PropertyDescriptorCollection cols, params PropertyDescriptor[] items)
{
PropertyDescriptor[] array = new PropertyDescriptor[cols.Count + items.Length];
cols.CopyTo(array, 0);
for (int i = 0; i < items.Length; i++)
array[cols.Count + i] = items[i];
PropertyDescriptorCollection newcols = new PropertyDescriptorCollection(array);
return newcols;
}
}
는 MyTypeDescriptionProvider
internal sealed class MyTypeDescriptionProvider : TypeDescriptionProvider
{
private ICustomTypeDescriptor td;
public MyTypeDescriptionProvider()
: this(TypeDescriptor.GetProvider(typeof(MyFieldsClass)))
{
}
public MyTypeDescriptionProvider(TypeDescriptionProvider parent)
: base(parent)
{
}
public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance)
{
return td ?? (td = new MyCustomTypeDescriptor(base.GetTypeDescriptor(objectType, instance)));
}
}
사용 예 :
dataGridView1.DataSource = new List<MyFieldsClass>(new[] { new MyFieldsClass { IntField = 1, DoubleField = 10.0 } });
그것은이 허용하지 않는 것 기술적으로하지 DataGridView에 그. 내부적으로 사용되는 CurrencyManager 클래스는 필드가 아닌 속성 만 허용합니다. 네가 묻고있는 일을하는 좋은 방법을 생각할 수 없다. – test
그리드가보기 전용 인 경우 LINQ를 사용하여 IEnumerable 개체를 필드 등에서 속성을 만드는 익명 개체로 변환 할 수 있습니다. – LarsTech