2013-06-23 4 views
2

두 개의 개체 (List LedgerEntries 및 List BuyerSellers)를 단일 DataGridView에 바인딩하려고합니다. LedgerEntry에는 Buyer_Seller에 대한 속성이 포함되어 있으며 최종 사용자가 DataGridView에서 ComboBox (BuyerSellers 제네릭 컬렉션으로 채워짐)에서 Buyer_Seller를 선택하고 LedgerEntries 문자열 BuyerSeller 속성을 Buyer_Seller 문자열 Name 속성으로 설정하려고합니다.DataGridView에서 다른 개체 속성의 값으로 개체 속성 초기화

지금은 하나의 BindingSource 만 사용하고 있으며 내 열은 정의하지 않았습니다. DGV에 바인딩 된 객체를 기반으로 자동 생성됩니다. 내가 잃어버린 부분은 한 객체의 속성이 다른 객체에 의해 채워진 콤보 상자의 값으로 초기화되도록하는 방법입니다. 모든 도움을 미리 감사드립니다.

답변

0

내가 여기 찾던 찾았 http://social.msdn.microsoft.com/Forums/vstudio/en-US/62ddde6c-ed96-4696-a5d4-ef52e32ccbf7/binding-of-datagridviewcomboboxcolumn-when-using-object-binding

public partial class Form1 : Form 
{ 
    List<LedgerEntry> ledgerEntries = new List<LedgerEntry>(); 
    List<Address> addresses = new List<Address>(); 
    BindingSource entrySource = new BindingSource(); 
    BindingSource adSource = new BindingSource(); 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     entrySource.DataSource = ledgerEntries; 
     adSource.DataSource = addresses; 

     DataGridViewComboBoxColumn adr = new DataGridViewComboBoxColumn(); 
     adr.DataPropertyName = "Address"; 
     adr.DataSource = adSource; 
     adr.DisplayMember = "OrganizationName"; 
     adr.HeaderText = "Organization"; 
     adr.ValueMember = "Ref"; 

     ledger.Columns.Add(adr); 
     ledger.DataSource = entrySource; 

     addresses.Add(new Address("Test1", "1234", 5678)); 
     addresses.Add(new Address("Test2", "2345", 9876)); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     foreach (LedgerEntry le in ledgerEntries) 
      MessageBox.Show(le.Address.OrganizationName + " // " + le.Description); 
    } 
} 

public class LedgerEntry 
{ 
    public string Description { get; set; } 
    public Address Address { get; set; } 
} 

public class Address 
{ 
    public string OrganizationName { get; set; } 
    public string StreetAddress { get; set; } 
    public int ZipCode { get; set; } 

    public Address(string orgname, string addr, int zip) 
    { 
     OrganizationName = orgname; 
     StreetAddress = addr; 
     ZipCode = zip; 
    } 

    public Address Ref 
    { 
     get { return this; } 
     set { Ref = value; } 
    } 

    public override string ToString() 
    { 
     return this.OrganizationName; 
    } 
}