Binding custom business object list using a combo box

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • g18c@hotmail.com

    #1

    Binding custom business object list using a combo box

    Hi, i have a class which has a property which is a list of items. I
    want to be able to set this item list from a set of items (in this case
    list2 as shown in the code below) from a drop down combo box. This will
    allow me to enter valid items in a seperate form, then when adding a
    new Class1 object i can select from one of the valid Class2 items in
    the dropdown list. I hope this makes sense, i have got partway there
    but i have lost synchronisation and i dont think the code is correct. I
    have posted some of the code below, and posted the full project source
    at www.monsterserve.net/test.zip

    using System;
    using System.Collecti ons.Generic;
    using System.Componen tModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows. Forms;

    namespace Test
    {
    public partial class Form1 : Form
    {
    static public BindingList<Cla ss1> list = new
    BindingList<Cla ss1>();
    static public BindingList<Cla ss2> list2 = new
    BindingList<Cla ss2>();

    public Form1()
    {
    InitializeCompo nent();

    list2.Add(new Class2("bill")) ;
    list2.Add(new Class2("fred")) ;

    m_Class1Binding Source.DataSour ce = list;

    m_Class2Binding Source.DataSour ce = m_Class1Binding Source;
    m_Class2Binding Source.DataMemb er = "MyClass";

    nameDataGridVie wTextBoxColumn1 .DataSource = list2;
    nameDataGridVie wTextBoxColumn1 .DisplayMember = "Name";
    }
    }

    public class Class2
    {
    string m_Name;
    public string Name
    {
    get { return m_Name; }
    set { m_Name = value; }
    }

    public Class2(string name)
    {
    Name = name;
    }
    }

    public class Class1
    {
    public Class1()
    {
    m_Class2 = Form1.list2[0];
    }

    string m_Name;
    public string Name
    {
    get { return m_Name; }
    set { m_Name = value; }
    }

    Class2 m_Class2;

    public Class2 MyClass
    {
    get { return m_Class2; }
    set { m_Class2 = value; }
    }
    }
    }

    I would also like to have a list, where i can use a combobox to select
    from a set of valid pre-existing items. I have seen from the book "Data
    Binding With Windows Forms 2.0" on chapter 9 it almost does what i want
    but there is not preexisting list.

    Any pointers much appreciated, with my newb skills i am very stuck!!

    Cheers,

    Chris

Working...