次のコードを使用して、datagridview でデータソースを使用するクラスに TypeDescriptor Provider を追加しようとしています。
private void FrmTarifadoVariantes_Load(object sender, EventArgs e)
{
System.ComponentModel.TypeDescriptor.AddProvider((new MyTypeDescriptionProvider<TarifadoVarianteBE>()), typeof(TarifadoBloquesBE));
System.ComponentModel.TypeDescriptor.AddProvider((new MyTypeDescriptionProvider<BloquePrendaBE>()), typeof(TarifadoBloquesBE));
}
質問1:
最初はすべて問題ありませんが、フォームを閉じて再度開くと、データプロバイダー記述子が列を2回、最初にプロバイダーを削除してから再度追加しようとしましたが、うまくいきません。
Webで検索したMyTypeDescriptionProviderがあります:
public class MyTypeDescriptionProvider<T> : TypeDescriptionProvider
{
private ICustomTypeDescriptor td;
public MyTypeDescriptionProvider()
: this(TypeDescriptor.GetProvider(typeof(TarifadoBloquesBE)))
{
}
public MyTypeDescriptionProvider(TypeDescriptionProvider parent)
: base(parent)
{
}
public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance)
{
if (td == null)
{
td = base.GetTypeDescriptor(objectType, instance);
td = new MyCustomTypeDescriptor(td, typeof(T));
}
return td;
}
}
public class SubPropertyDescriptor : PropertyDescriptor
{
private PropertyDescriptor _subPD;
private PropertyDescriptor _parentPD;
public SubPropertyDescriptor(PropertyDescriptor parentPD, PropertyDescriptor subPD, string pdname)
: base(pdname, null)
{
_subPD = subPD;
_parentPD = parentPD;
}
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 _parentPD.ComponentType; }
}
public override Type PropertyType { get { return _subPD.PropertyType; } }
public override object GetValue(object component)
{
return _subPD.GetValue(_parentPD.GetValue(component));
}
public override void SetValue(object component, object value)
{
_subPD.SetValue(_parentPD.GetValue(component), value);
OnValueChanged(component, EventArgs.Empty);
}
}
public class MyCustomTypeDescriptor : CustomTypeDescriptor
{
Type typeProperty;
public MyCustomTypeDescriptor(ICustomTypeDescriptor parent, Type type)
: base(parent)
{
typeProperty = type;
}
public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
PropertyDescriptorCollection cols = base.GetProperties(attributes);
string propertyName = "";
foreach (PropertyDescriptor col in cols)
{
//se agregó si el attributo es browsable
if (col.IsBrowsable)
{
if (col.PropertyType.Name == typeProperty.Name)
{
propertyName = col.Name;
break;
}
}
}
PropertyDescriptor pd = cols[propertyName];
PropertyDescriptorCollection children = pd.GetChildProperties();
PropertyDescriptor[] array = new PropertyDescriptor[cols.Count + children.Count];
int count = cols.Count;
cols.CopyTo(array, 0);
foreach (PropertyDescriptor cpd in children)
{
if (cpd.IsBrowsable)
{
array[count] = new SubPropertyDescriptor(pd, cpd, pd.Name + "_" + cpd.Name);
count++;
}
}
//PropertyDescriptor[] arrayTmp = new PropertyDescriptor[count];
Array.Resize(ref array, count);
PropertyDescriptorCollection newcols = new PropertyDescriptorCollection(array);
return newcols;
}
}
質問2:
MyTypeDescriptionProvider を動的にするにはどうすればよいですか。確認できる場合は、この部分で GetProvider のクラス (TarifadoBloquesBE) をハードコーディングします。
private ICustomTypeDescriptor td;
public MyTypeDescriptionProvider()
: this(TypeDescriptor.GetProvider(typeof(TarifadoBloquesBE)))
{
}
プロバイダーとなるクラスの送信を許可したい。ありがとうございました。
2 つの画像を添付しました。最初の 1 つは問題ありません。次の 1 つは、スクロール バーを確認するだけで列を 2 回確認できます。
ここですべて良い
重複する列