2017-03-16 72 views
0

我想分配多个字段值组成的成员在组合框中。从代码中可以看出,分配给value成员的当前字符串是“title”,并且在cboCustomers_SelectionChangeCommited事件中,您可以看到文本框已分配了选定的值。从一个组合框C多个ValueMembers#

我希望实现的是将2个更多的字段赋值给值成员(“firstname”,“lastname”),并为这两个值指定两个更多的文本框。

我希望我已经清楚。如果不是,请说明,我会尝试重新解释。

private void Form3_Load(object sender, EventArgs e) 
        { 
         try 
         { 
          dbConn = new OleDbConnection(conString); 
          sql = @"SELECT customer.title, firstname, lastname, product.name, account.balance 
            FROM (account INNER JOIN customer ON account.custid = customer.custid) INNER JOIN product ON account.prodid = product.prodid;"; 

          daItems = new OleDbDataAdapter(sql, dbConn); 
          daItems.Fill(dtAccBal); 


          cboCustomers.DataSource = (dtAccBal); 
          cboCustomers.DisplayMember = "firstname"; 
          cboCustomers.ValueMember = "title"; 
          cboCustomers.SelectedIndex = -1; 

         } 
         catch (Exception ex) 
         { 
          MessageBox.Show(ex.Message, "Error!"); 
         } 
        } 


        private void cboCustomers_SelectionChangeCommitted(object sender, EventArgs e) 
        { 
         if (cboCustomers.SelectedIndex > -1) 
         { 
          try 
          { 
           txtTitle.Text = cboCustomers.SelectedValue.ToString(); 



          } 
          catch (Exception ex) 
          { 
           MessageBox.Show(ex.Message, "Error!"); 
          } 
         } 
        } 
       } 
+0

这是错的,你不应该使用像这样的多个值的下拉项。 –

+0

你能提出一种替代方法吗? –

回答

0

您可以使用实现INotifyPropertyChanged接口对象,并添加数据绑定到你的文本框。

示例:实现INotifyPropertyChanged

public class Person : INotifyPropertyChanged 
{ 
    private string _firstName; 
    public string FirstName 
    { 
     get { return _firstName; } 
     set 
     { 
      if (value == _firstName) return; 
      _firstName = value; 
      OnPropertyChanged(); 
     } 
    } 

    private string _lastName; 
    public string LastName 
    { 
     get { return _lastName; } 
     set 
     { 
      if (value == _lastName) return; 
      _lastName = value; 
      OnPropertyChanged(); 
     } 
    } 

    public override string ToString() => $"{FirstName} {LastName}"; 

    public event PropertyChangedEventHandler PropertyChanged; 
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) 
    { 
     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

添加上形式ComboBox “comboBoxPeople” 和两个TextBoxes “textBoxFirstName”, “textBoxLastName”

类Person。形式的

更改构造:

public Form1() 
{ 
    InitializeComponent(); 

    var people = new BindingList<Person> { 
     new Person() { FirstName = "Peter", LastName = "Pan" }, 
     new Person() { FirstName = "Tinker", LastName = "Bell" }, 
     new Person() { FirstName = "James", LastName = "Hook" }, 
     new Person() { FirstName = "Wendy", LastName = "Darling" }, 
    }; 

    var bindingSource = new BindingSource() { DataSource = people }; 

    comboBoxPeople.DataSource = bindingSource; 
    textBoxFirstName.DataBindings.Add(nameof(TextBox.Text), bindingSource, nameof(Person.FirstName), false, DataSourceUpdateMode.OnPropertyChanged); 
    textBoxLastName.DataBindings.Add(nameof(TextBox.Text), bindingSource, nameof(Person.LastName), false, DataSourceUpdateMode.OnPropertyChanged); 
} 

现在你拥有一个充满人的组合框。选择其他人后,文本框将自动填入适当的数据。