2009-05-09 7 views
1

예 : 양식에 Contact 개체가 있습니다 (아래 참조). BindingSource의 데이터 소스를 Contact.Addresses 속성으로 설정할 수 있습니까?BindingSource의 DataDource가 양식의 개체이고 양식의 개체가 될 수 있습니까?

AddressCollection 클래스는 BindingList를 구현하므로 Contact 클래스에 의해 캡슐화되지 않았을 때 바인딩 바인딩에 문제가 없습니다. 한마디로

public class Contact : IComparable<Contact>, IComponent 
{ 
    #region Properties 
    private AddressCollection addresses = new AddressCollection(); 
    private ContactNumberCollection contactNumbers = new ContactNumberCollection(); 

    public int ContactId { get; set; } 
    public string Title { get; set; } 
    public string Forename { get; set; } 
    public string MiddleName { get; set; } 
    public string Surname { get; set; } 
    public string JobTitle { get; set; } 
    public DateTime DateAdded { get; set; } 
    public DateTime LastUpdate { get; set; } 

    public AddressCollection Addresses 
    { 
     get { return addresses; } 
     set { addresses = value; } 
    } 
    public ContactNumberCollection ContactNumbers 
    { 
     get { return contactNumbers; } 
     set { contactNumbers = value; } 
    } 
    #endregion 

    #region Constructors 
    public Contact() 
    { 
     DateAdded = DateTime.Now; 
     LastUpdate = DateTime.Now; 
    } 

    #endregion 

    public int CompareTo(Contact other) 
    { 
     return FullName.CompareTo(other.FullName); 
    } 

    #region IComponent Objects 
    private ISite mSite; 

    public event EventHandler Disposed; 

    public virtual void Dispose() 
    { 
     if ((mSite != null) && (mSite.Container != null)) 
     { 
      mSite.Container.Remove(this); 
     } 

     Disposed(this, System.EventArgs.Empty); 
    } 

    public ISite Site 
    { 
     get 
     { 
      return mSite; 
     } 
     set 
     { 
      mSite = value; 
     } 
    } 
    #endregion 
} 

감사 앤서니

+0

귀하의 질문 - 당신은 컬렉션에 대한 필요없이 하나의 항목에 쉽게 바인딩 할 수 있습니다. –

답변

0

, 그래 당신은 할 수 있습니다. 그러나 연락처를 변경해야하는 경우 데이터 소스를 연락처 (또는 연락처 목록)로 설정하고 DataMember를 "주소"로 사용할 수 있습니다.

다음은 이러한 바인딩을 보여주는 간단한 예입니다. 귀하의 요점을 놓친 경우 명확히하십시오 :

using System; 
using System.ComponentModel; 
using System.Windows.Forms; 
class AddressCollection : BindingList<Address> 
{ 
    public void Add(string line1, string zip) 
    { // just for convenience 
     Add(new Address { Line1 = line1, Zip = zip}); 
    } 
} 

class Address 
{ 
    public string Line1 { get; set; } 
    public string Zip { get; set; } 

    public override string ToString() 
    { 
     return Line1; 
    } 
} 
class Contact 
{ 
    public string Name { get; set; } 
    private readonly AddressCollection addresses = new AddressCollection(); 
    public AddressCollection Addresses { get { return addresses; } } 
    public override string ToString() 
    { 
     return Name; 
    } 
} 

static class Program 
{ 
    [STAThread] 
    static void Main() 
    { 
     BindingList<Contact> contacts = new BindingList<Contact> 
     { 
      new Contact { Name = "Fred", Addresses = { {"123 Some road", "AB1"}}}, 
      new Contact { Name = "Barney", Addresses = { {"456 A Street", "CD2"}, {"789 The Lane", "EF3"}}}     
     }; 
     BindingSource bs = new BindingSource(contacts, ""); 

     Form form = new Form { 
      Controls = { 
           new DataGridView { Dock = DockStyle.Fill, DataSource = bs, DataMember = "Addresses" }, 
       new ComboBox { Dock = DockStyle.Top, DataSource = bs, DisplayMember = "Name" }, 
       new TextBox { Dock = DockStyle.Bottom, DataBindings = { {"Text", bs, "Addresses.Zip"}}} 
      } 
     }; 
     Application.Run(form); 

    } 
} 
+0

이것은 제가 염두에 두었던 것입니다. 유일한 문제는 양식에 있으며 연락처는 다음과 같이 정의됩니다. BusinessObjects.Contact currentCustomer; 고객 컬렉션을 보유하는 대신 나는 하나의 아이템을 가진 콜렉션을 간단하게 가질 수 있지만 이것은 단지 깔끔하지 않은 것처럼 보인다. –