Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
250
Databinding collection to grid and edit subobect using a combobox in the grid (while maintaining databinding)
posted

Normal 0 21 false false false FR-CA X-NONE X-NONE MicrosoftInternetExplorer4 /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Tableau Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:"Times New Roman"; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin;} Here is what i want to do :

I have 2 objects (see classes bellow, I also added my form code) : PartyType and Personne

Personne has a PartyType property.

I want to be able to databind a collection<Personne> to your grid.

I want to be able to edit the PartyType of a Personne object using a combobox in the grid that would list the available type and style be databound to the underlying collection.

So instead of having a textbox showing : Test.DataObject.PartyType

I would like to have a combobox showing the value of PartyType.ID for the Personne object of that row. The comboBox still need to be databound to the underlying datasource.

 


Is this possible ?

 

using System;
using System.ComponentModel;

namespace Test.DataObject
{
    public partial class PartyType
    {
        private string _description;
        private string _id;

        public event PropertyChangedEventHandler PropertyChanged;

        public string Description
        {
            get { return _description; }
            set
            {
                if (value != _description)
                {
                    _description = value;
                    NotifyPropertyChanged("Description");
                }
            }
        }

        public string ID
        {
            get { return _id; }
            set
            {
                if (value != _id)
                {
                    _id = value;
                    NotifyPropertyChanged("ID");
                }
            }
        }

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

    }
}

 

 

using System;
using System.ComponentModel;

namespace Test.DataObject
{
    public partial class Personne
    {
        private string _id;
        private string _nom;
        private PartyType _partyType;

        public event PropertyChangedEventHandler PropertyChanged;

        public string Nom
        {
            get { return _nom; }
            set
            {
                if (value != _nom)
                {
                    _nom = value;
                    NotifyPropertyChanged("Nom");
                }
            }
        }

        public PartyType PersonnePartyType
        {
            get { return _partyType; }
           
            set
            {
                if(value != _partyType)
                {
                    _partyType = value;

                    NotifyPropertyChanged("PersonnePartyType");
                }
               
            }
        }

        public string ID
        {
            get { return _id; }
            set
            {
                if (value != _id)
                {
                    _id = value;
                    NotifyPropertyChanged("ID");
                }
            }
        }

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
    }
}

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Test.DataObject;

namespace Test
{
    public partial class Form1 : Form
    {
        private Personne _personne;
        private BindingList<Personne> _listPersonne;
        private BindingList<PartyType> _listPartyType;

        private readonly PartyType _personnePartyType;
        private readonly PartyType _CompagniePartyType;
        private Personne _personne1;
        private Personne _personne2;
        private Personne _personne3;

        public Form1()
        {

            InitializeComponent();

            _personnePartyType = new PartyType { Description = "Personne", ID = "1" };

            _CompagniePartyType = new PartyType { Description = "Compagnie", ID = "2" };

            _listPartyType = new BindingList<PartyType> { _personnePartyType, _CompagniePartyType };
        }

        private void ultraButton1_Click(object sender, EventArgs e)
        {
            _personne1 = new Personne { Nom = "Mathieu", ID = "M", PersonnePartyType = _CompagniePartyType };

            _personne2 = new Personne { ID = "E", Nom = "Eli", PersonnePartyType = _personnePartyType };

            _personne3 = new Personne { ID = "PL", Nom = "Pierre-Luc", PersonnePartyType = _CompagniePartyType };

            _listPersonne = new BindingList<Personne> { _personne1, _personne2 };

            grid.DataSource = _listPersonne;

           
        }

        private void ultraButton2_Click(object sender, EventArgs e)
        {
            _listPersonne.Add(_personne3);
        }

        private void ultraButton3_Click(object sender, EventArgs e)
        {
            foreach (Personne personne in _listPersonne)
            {
                if (personne.ID == "PL")
                {
                    personne.PersonnePartyType = _personnePartyType;
                }
            }
        }
    }
}
 

Parents
No Data
Reply
  • 2334
    Verified Answer
    posted
    create a ValueList that contains a list of the possible PartyType objects. then set the ValueList property of the Column to this list.
Children
No Data