来自一个 ComboBox C# 的多个 ValueMembers
Multiple ValueMembers From One ComboBox C#
我想将 1 个以上的字段分配给组合框中的值成员。从代码中可以看出,当前赋给value成员的字符串是"title",在cboCustomers_SelectionChangeCommited事件中,可以看到一个文本框已经赋值了选中的值
我希望实现的是将另外 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!");
}
}
}
}
您可以使用实现 INotifyPropertyChanged
接口的对象并将 DataBindings 添加到您的文本框。
示例:
Class 实施者 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);
}
现在你有一个装满人的组合框。在 select 之后,另一个人的文本框将自动填充适当的数据。
我想将 1 个以上的字段分配给组合框中的值成员。从代码中可以看出,当前赋给value成员的字符串是"title",在cboCustomers_SelectionChangeCommited事件中,可以看到一个文本框已经赋值了选中的值
我希望实现的是将另外 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!");
}
}
}
}
您可以使用实现 INotifyPropertyChanged
接口的对象并将 DataBindings 添加到您的文本框。
示例:
Class 实施者 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);
}
现在你有一个装满人的组合框。在 select 之后,另一个人的文本框将自动填充适当的数据。