2

2 つのオブジェクト (List LedgerEntries と List BuyerSellers) を 1 つの DataGridView にバインドしようとしています。LedgerEntry には Buyer_Seller のプロパティが含まれており、エンドユーザーが DataGridView のコンボボックス (BuyerSellers ジェネリック コレクションによって設定された) から Buyer_Seller を選択し、LedgerEntries 文字列 BuyerSeller プロパティが Buyer_Seller 文字列 Name プロパティに設定されるようにしたいと考えています。

現時点では、BindingSource を 1 つだけ使用しており、独自の列を定義していません。これらは、DGV にバインドされているオブジェクトに基づいて自動生成されます。私が少し迷っているのは、あるオブジェクトのプロパティが、別のオブジェクトによって取り込まれたコンボボックスの値に確実に初期化されるようにする方法です。助けてくれてありがとう。

4

1 に答える 1

0

ここで探していたものを見つけました: http://social.msdn.microsoft.com/Forums/vstudio/en-US/62ddde6c-ed96-4696-a5d4-ef52e32ccbf7/binding-of-datagridviewcomboboxcolumn-when-using-object-バインディング

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;
    }
}
于 2013-06-23T19:12:43.633 に答える