Réponse acceptée !
Bonjour
J'ai trouvé une solution, je ne sais pas si c'est la plus simple (je pense pas mais ça marche). J'utilise les PropertyDescriptor. J'ai vu ça dans les WebCast de Mistu sur le DataBinding Avancé.
En gros, j'ai créer un MonObjetBindingSource propre à mon Objet (bien mon bindinsource dérive de BindingSource) dans lequel j'ai ajouté de nouvelles propriétés
Je vous mets un exemple :
Ma classe CustomClientBindingSource
class CustomClientBindingSource : BindingSource {
public delegate object PropertyEventHandler(object sender, object row, string propertyName, Type propertyType); public event PropertyEventHandler PropertyEval;
protected internal object OnPropertyEval(object row, string propertyName, Type propertyType) { if (!DesignMode) { if (PropertyEval != null) { return PropertyEval(this, row, propertyName, propertyType); } } return null; }
public override System.ComponentModel.PropertyDescriptorCollection GetItemProperties(System.ComponentModel.PropertyDescriptor[] listAccessors) { PropertyDescriptorCollection sourceProps; if (this.DataSource == null) sourceProps = PropertyDescriptorCollection.Empty;
object obj1 = ListBindingHelper.GetList(this.DataSource); if ((obj1 is ITypedList) && !string.IsNullOrEmpty(this.DataMember)) { sourceProps = ListBindingHelper.GetListItemProperties(obj1, this.DataMember, listAccessors); } else sourceProps = ListBindingHelper.GetListItemProperties(obj1, listAccessors);
PropertyDescriptor[] props = new PropertyDescriptor[sourceProps.Count + 2]; for (int i = 0; i < sourceProps.Count; i++) { props[i] = sourceProps[i]; } CustomTypePropertyDescriptor cust = new CustomTypePropertyDescriptor(this,GetType(), "Fake", typeof(string)); props[sourceProps.Count] = cust; CustomTypePropertyDescriptor cust1 = new CustomTypePropertyDescriptor(this,GetType(), "Adresse", typeof(string)); props[sourceProps.Count + 1] = cust1;
return new PropertyDescriptorCollection(props); }
private class CustomTypePropertyDescriptor : PropertyDescriptor { public CustomTypePropertyDescriptor(CustomClientBindingSource source, Type componentType, string propertyName, Type propertyType) : base(propertyName, null) { this.propertyName = propertyName; this.propertyType = propertyType; this.componentType = componentType; this.source = source; }
private string propertyName; private Type propertyType; private Type componentType; private CustomClientBindingSource source;
public override object GetValue(object component) { return source.OnPropertyEval(component, propertyName, propertyType); }
protected override Attribute[] AttributeArray { get { return null; } set { } } public override Type ComponentType { get { return componentType; } } public override bool IsReadOnly { get { return true; } } public override Type PropertyType { get { return propertyType; } } public override bool CanResetValue(object component) { return false; } public override void ResetValue(object component) { } public override void SetValue(object component, object value) { } public override bool ShouldSerializeValue(object component) { return false; } } }
Ma form ou j'ai placé un datagridview que j'ai binder avec mon ClientBindingSource :
public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e) { Adresse adr = new Adresse(1, "Rue des marguerites"); ArrayList list = new ArrayList(); list.Add(new Client("Toto", "Seb", adr)); list.Add(new Client("Titi", "Franck", adr)); customClientBindingSource1.DataSource = list; }
private object customClientBindingSource1_PropertyEval(object sender, object row, string propertyName, Type propertyType) { switch (propertyName) { case "Fake": return ((Client)row).Nom + " " + ((Client)row).Prenom; case "Adresse": return ((Client)row).AdresseClient.NumeroAdresse + " " + ((Client)row).AdresseClient.Rue; } return null; } }
Dans l'évenement PropertyEval, j'alimente les nouvelles propriétés.
Voilà
|