2017-03-16 1 views
0

콤보 상자에서 두 개 이상의 필드를 값 멤버에 할당하려고합니다. 코드에서 볼 수 있듯이 value 멤버에 할당 된 현재 문자열은 "title"이며 cboCustomers_SelectionChangeCommited 이벤트에서 텍스트 상자에 선택한 값이 할당 된 것을 볼 수 있습니다.하나의 ComboBox에서 여러 ValueMembers C#

내가 달성하기를 희망하는 것은 value member ("firstname", "lastname")에 할당 된 2 개의 추가 필드가 있고 두 개의 추가 텍스트 상자에이 값이 할당되어 있어야합니다.

나는 분명히했으면 좋겠다. 그렇지 않다면 명시하고 다시 설명하려고 시도 할 것입니다.

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 인터페이스를 구현하는 객체를 사용하고 TextBox에 DataBindings를 추가 할 수 있습니다.

예 : 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"에 추가 구현

클래스 사람. 형태의

변경 생성자 :

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); 
} 

이제 당신은 사람들로 가득 콤보 상자가 있습니다. 다른 사람을 선택하면 텍스트 상자에 적절한 데이터가 자동으로 채워집니다.

관련 문제