来自一个组合C#的多个估值



我想在Combobox中分配一个以上的字段为值。从代码可以看出,分配给值成员的当前字符串为" title",在cbocustomers_selectionchangecommited事件中,您可以看到已分配了所选值的文本框。

我希望实现的目标是将两个字段分配给值成员(" 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!");
                            }
                        }
                    }
                }

您可以使用实现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"。

更改形式的构造函数:

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

现在您有一个装满人的组合盒。选择另一个人的文本框将自动使用适当的数据填充。

相关内容

  • 没有找到相关文章

最新更新