我正在尝试为我在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:
第一次一切都很好,但是当我关闭表单并再次打开时,数据提供程序描述符两列,我试图首先删除提供程序,然后再次添加,但它没有工作
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 dinamyc,我的意思是,如果你可以检查,我在这部分硬编码GetProvider的类(TarifadoBloquesBE):
private ICustomTypeDescriptor td;
public MyTypeDescriptionProvider()
: this(TypeDescriptor.GetProvider(typeof(TarifadoBloquesBE)))
{
}
我想允许发送哪个类将成为提供者。谢谢。
我附上了两张图片,第一张是好的,下一张你可以检查两列,只看到滚动条。
一切都很好
重复列