IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Windows Forms Discussion :

Afficher une collection d'objets dans une datagridview


Sujet :

Windows Forms

  1. #1
    Membre régulier
    Inscrit en
    Mars 2007
    Messages
    221
    Détails du profil
    Informations forums :
    Inscription : Mars 2007
    Messages : 221
    Points : 88
    Points
    88
    Par défaut Afficher une collection d'objets dans une datagridview
    Bonjour,

    Mon problème est le suivant. Je cherche à afficher dans un datagridview les différents éléments d'une collection d'objets "LigneFactureClass". J'utilise donc le bindingsource auquel j'affecte ma collection.
    Lorsque j'exécute mon appli, mon datagridview est bien chargé avec les éléments de ma collection.
    Mon problème est que les objets de ma collection possèdent un attribut d'un type non primitif (type ProduitClass que j'ai créé)

    Mon datagridview affiche bien les valeurs des attributs simples (n° facture, n° ligne) mais pour mon attribut de type ProduitClasse, il affiche "ProduitClass".
    Comment faire pour afficher la valeur de l'attribut Produit_cod de mon attribut de type ProduitClasse

    public class ProduitClass
    {
    private System.String _Produit_cod = "";
    private System.String _Produit_ref = "";
    private System.String _Produit_des = "";
    ...
    public String Produit_cod {...}
    public String Produit_ref {...}
    public String Produit_des {...}

    }

    J'ai une classe LigneFacture :
    public class LigneFactureClass
    {
    private System.UInt32 _DetailId = 0;
    private System.UInt32 _FactureId = 0;
    private Produit _Produit = null;

    public System.UInt32 _DetailId {...}
    public System.UInt32 _FactureId {...}
    public ProduitClass Produit {...}
    ...
    }


    J'espère avoir été assez clair, merci

    Si quelqu'un à des exemples concrets sur le bindingsource (sans passer par ADO.Net, Dataset ...), je suis preneur

    Merci à tous

  2. #2
    Membre expérimenté Avatar de Mose
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    1 143
    Détails du profil
    Informations personnelles :
    Âge : 47
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 1 143
    Points : 1 379
    Points
    1 379
    Par défaut
    Soit tu créé un type de colonne spécial pour ton Type à toi.
    Soit tu override "ToString()" dans ton type perso.
    Soit tu trouves comment désigner, dans la colonne, le membre à appeler pour déterminer le texte à afficher. Y'a un DataPropertyName pour la donnée, mais pour la valeur affichée je sais pas.

  3. #3
    Nouveau membre du Club
    Inscrit en
    Février 2007
    Messages
    49
    Détails du profil
    Informations forums :
    Inscription : Février 2007
    Messages : 49
    Points : 35
    Points
    35
    Par défaut
    Les colonnes sont générées automatiquement ou tu les as définies à la main toi même?
    Une colonne avec le dataPropertyName du type Produit.Produit_cod ça ne fonctionne pas?

  4. #4
    Membre régulier
    Inscrit en
    Mars 2007
    Messages
    221
    Détails du profil
    Informations forums :
    Inscription : Mars 2007
    Messages : 221
    Points : 88
    Points
    88
    Par défaut
    Bonjour,

    Tout d'abord, merci pour vos réponses.
    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à

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 1
    Dernier message: 26/12/2010, 21h20
  2. [VB.NET] Suppression d'objets dans une collection
    Par master56 dans le forum VB.NET
    Réponses: 7
    Dernier message: 03/06/2010, 21h46
  3. Hibernate + suppression d'objets dans une collection
    Par Saiyan54 dans le forum Hibernate
    Réponses: 2
    Dernier message: 15/12/2006, 15h39
  4. Modifier une collection d'objets dans un formulaire
    Par GiveMeAName dans le forum Struts 1
    Réponses: 6
    Dernier message: 30/08/2006, 13h30
  5. Réponses: 6
    Dernier message: 24/03/2006, 09h22

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo