gpt4 book ai didi

c# - ComboBox 数据源动态变化

转载 作者:行者123 更新时间:2023-11-30 23:05:38 25 4
gpt4 key购买 nike

首先:我有 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();
}

最佳答案

有了关于组合框属性的新信息,我终于可以重现您的问题。似乎 Sorted 属性导致了这个问题:

this.CustomersName_ComBx.Sorted = true;

解决方法是设置为false

this.CustomersName_ComBx.Sorted = false;

要让显示仍然有序,只需在初始化 DataSource 时使用 OrderBy:

CustomersPhone_ComBx.DataSource = Customers.Select(Customer => Customer.Phone)
.OrderBy(x=>x).Distinct().ToList();
CustomersName_ComBx.DataSource = Customers.Select(Customer => Customer.Name)
.OrderBy(x => x).ToList();

可以在属性 Sorted 的文档中找到(某种解释)

在备注部分,它说:

This property specifies whether the ComboBox sorts existing entries and add new entries to the appropriate sorted position in the list. You can use this property to automatically sort items in a ComboBox. As items are added to a sorted ComboBox, the items are moved to the appropriate location in the sorted list.

我想当您更改 DataSource 时,新元素只是简单地放在旧元素的位置。但是实际的 DataSource 现在有更少的元素。您可以在调试器中检查它。 DataSource 的计数在过滤集合分配后会变少!

我的第二个猜测是,如果您设置 Sorted = true,它实际上会在初始初始化后忽略 DataSource 集合,并且从这一刻起,它只会显示其中的内容Items 集合。调试器显示 Items.Count 在分配过滤后的 DataSource 后保持不变,但 DataSource.Count 已更改。这似乎是一种模棱两可的状态。您不能修改 Items 集合,因为 ComboBox 是数据绑定(bind)的,但您不能显示 DataSource,因为 Sorted 是设置为真。实际上,评论是这样说的:

Attempting to set the Sorted property on a data-bound control raises an ArgumentException. You must sort the data using the underlying data model.

但它没有说明当您在绑定(bind)之前设置它时会发生什么!

关于c# - ComboBox 数据源动态变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48691615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com