【发布时间】:2018-07-19 09:14:04
【问题描述】:
首先:我有 List 填充了 Customer
的对象类型List<Customer> Customers = new List<Customer>();
第二:我有 2 个 ComboBox,一个用于 Customer Name,另一个用于 Customer Phone。并将 DataSource 设置为 Customers 列表。
CustomersPhone_ComBx.DataSource = Customers.Select(Customer => Customer.Phone).ToList();
CustomersName_ComBx.DataSource = Customers.Select(Customer => Customer.Name).ToList();
第三:当用户更改 Phone ComboBox 中的选定项目时,我想过滤 Name ComboBox 中的名称,该名称与选择的电话号码相同。 (因为可能是注册了多个名字的电话号码)。我有这个代码
private void CustomersPhone_ComBx_Leave(object sender, EventArgs e)
{
if (CustomersPhone_ComBx.Text != "")
CustomersName_ComBx.DataSource = Customers.Where(Customer => Customer.Phone == CustomersPhone_ComBx.Text).Select(Customer => Customer.Name).ToList();
else
CustomersName_ComBx.DataSource = Customers.Select(Customer => Customer.Name).ToList();
}
但是当我测试它并更改 Phone ComboBox 中的选定项目时,Name ComboBox 中没有任何变化。
更新 1
第四:如果我使用foreach,它可以像下面的代码一样正常工作,但不适用于DataSource
private void CustomersPhone_ComBx_SelectedIndexChanged(object sender, EventArgs e)
{
List<Customer> FilteredCustomers = Customers
.Where(Customer => Customer.Phone == CustomersPhone_ComBx.Text).ToList();
foreach (Customer C in FilteredCustomers)
CustomersName_ComBx.Items.Add(C.Name);
}
更新 2
2个ComboBox的这些属性,它们是一样的。
// CustomersName_ComBx
//
this.CustomersName_ComBx.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend;
this.CustomersName_ComBx.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;
this.CustomersName_ComBx.Dock = System.Windows.Forms.DockStyle.Fill;
this.CustomersName_ComBx.Font = new System.Drawing.Font("Janna LT", 12F, System.Drawing.FontStyle.Bold);
this.CustomersName_ComBx.FormattingEnabled = true;
this.CustomersName_ComBx.Location = new System.Drawing.Point(0, 65);
this.CustomersName_ComBx.Margin = new System.Windows.Forms.Padding(0, 7, 0, 0);
this.CustomersName_ComBx.Name = "CustomersName_ComBx";
this.CustomersName_ComBx.Size = new System.Drawing.Size(583, 46);
this.CustomersName_ComBx.Sorted = true;
this.CustomersName_ComBx.TabIndex = 52;
//
// CustomersPhone_ComBx
//
this.CustomersPhone_ComBx.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend;
this.CustomersPhone_ComBx.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;
this.CustomersPhone_ComBx.Dock = System.Windows.Forms.DockStyle.Fill;
this.CustomersPhone_ComBx.Font = new System.Drawing.Font("Janna LT", 12F, System.Drawing.FontStyle.Bold);
this.CustomersPhone_ComBx.FormattingEnabled = true;
this.CustomersPhone_ComBx.Location = new System.Drawing.Point(0, 7);
this.CustomersPhone_ComBx.Margin = new System.Windows.Forms.Padding(0, 7, 0, 0);
this.CustomersPhone_ComBx.Name = "CustomersPhone_ComBx";
this.CustomersPhone_ComBx.Size = new System.Drawing.Size(583, 46);
this.CustomersPhone_ComBx.Sorted = true;
this.CustomersPhone_ComBx.TabIndex = 51;
this.CustomersPhone_ComBx.SelectedIndexChanged += new System.EventHandler(this.CustomersPhone_ComBx_SelectedIndexChanged);
还有名为 NewReceipt_Delivery_Form 的表单以及我如何显示它。
NewReceipt_Delivery_Form NewReceipt_Delivery_Form = new NewReceipt_Delivery_Form();
NewReceipt_Delivery_Form.ChangeUI();
NewReceipt_Delivery_Form.ShowDialog();
和ChangeUI方法
private List<Customer> Customers = new List<Customer>();
public void ChangeUI()
{
OleDbCommand SelectCustomersCMD = new OleDbCommand("SELECT * FROM Customers ORDER BY [ID] ASC", Program.GeneralConnection);
OleDbDataReader SelectCustomersREAD = SelectCustomersCMD.ExecuteReader();
while (SelectCustomersREAD.Read())
{
Customer Customer = new Customer();
Customer.ID = Convert.ToInt32(SelectCustomersREAD[0].ToString());
/* And so on. */
Customers.Add(Customer);
}
SelectCustomersREAD.Close();
CustomersPhone_ComBx.DataSource = Customers.Select(Customer => Customer.Phone).ToList();
CustomersName_ComBx.DataSource = Customers.Select(Customer => Customer.Name).ToList();
CustomersPhone_ComBx.SelectedIndex = -1;
CustomersName_ComBx.SelectedIndex = -1;
}
SelectedIndexChanged 事件为Aleksandar's answer
private void CustomersPhone_ComBx_SelectedIndexChanged(object sender, EventArgs e)
{
string selectedPhone = (string)CustomersPhone_ComBx.SelectedItem;
if (!String.IsNullOrEmpty(selectedPhone))
CustomersName_ComBx.DataSource = Customers.Where(Customer => Customer.Phone == selectedPhone).Select(Customer => Customer.Name).ToList();
else
CustomersName_ComBx.DataSource = Customers.Select(Customer => Customer.Name).ToList();
}
【问题讨论】:
-
你测试了这两种情况吗?
if (DeliveryPhone_ComBx.Text != "")两者的结果一样吗? -
你是如何测试它的?你设置断点检查是否
CustomersPhone_ComBx_Leave?被执行了吗? -
是的,没有区别。无论情况如何,它仍然具有相同的 DataSource。如果我在 if 语句 之前添加这一行
CustomersName_ComBx.DataSource = null;,我找不到任何项目。 -
@MongZhu 是的,if语句完全没有问题。
-
您确定要休假而不是CustomersPhone_ComBx.selectedindexchanged?