C# Winforms ComboBox正在运行时添加行项目
更新如下C# Winforms ComboBox正在运行时添加行项目,c#,winforms,combobox,C#,Winforms,Combobox,更新如下 var billers = user.FindBillers(); billers.Insert(0, new user { strLName = "- -" }); billers.Insert(1, new user { strLName = "Provider Default" }); cboBillingProvdr.DataSource = null; cboBillingProvdr.DataSource = billers; cboBillingProvdr.Disp
var billers = user.FindBillers();
billers.Insert(0, new user { strLName = "- -" });
billers.Insert(1, new user { strLName = "Provider Default" });
cboBillingProvdr.DataSource = null;
cboBillingProvdr.DataSource = billers;
cboBillingProvdr.DisplayMember = "FullName";
cboBillingProvdr.ValueMember = "ID";
if (Currentuser.billerid == -1)
cboBillingProvdr.SelectedIndex = 1;
else if (CurrentUser.billerid > 0)
cboBillingProvdr.SelectedValue = CurrentUser.billerid ;
else
cboBillingProvdr.SelectedIndex = 0;
这就是如果数据库中有任何账单值(>0),则组合框
在表单上的显示方式:
- -
Provider Default
--
Provider 1
Provider 2
Provider 3
更新:我在user.findbillers中没有看到添加“-”的地方。但在同样的形式下,我相信下面的代码就是这样做的。当我评论它时,一切似乎都起作用了
var Physicians= user.FindBillers();
cboPhysician.DataSource = null;
Physicians.Insert(0, new user { strFName = "--" });
Physician.DataSource = Physicians;
Physician.DisplayMember = "FullName";
加载表单和随后的单击调用这些,但我不知道为什么一个会影响另一个 此范围中的代码没有添加“-”项,您需要检查user.FindBillers()方法(或将其作为编辑发布到此处)。应该有一行代码类似于:
/*假设返回集合的名称为“findBillers”*/
Insert(0,新用户{”--“});
/*或*/
添加(新用户{”--“});
这样,您发布的列表将返回如下结果:
--
Provider 1
Provider 2
Provider 3
然后,用你的两行代码
你以这份清单结束
- - (Inserted at 0)
Provider Default (Inserted at 1)
--
Provider 1
Provider 2
Provider 3
所以,你也必须这样做
a) 注释/删除user.FindBillers()中的行
b) 注释/删除对账单的两个插入,并在user.FindBillers()中添加提供程序默认值
编辑:您可以发布user.FindBillers()的代码,我将更新我的答案。已解决。我在user类中添加了另一个方法FindBillers1(),并调用它来填充第二个组合,它的工作听起来很疯狂。FindBillers和FindBillers1之间除了名称之外没有区别 我想知道。有道理。我将在user.中查找“-”,因此在同一个表单上有user.FindBillers,它将“-”添加到另一个组合框中,这似乎是问题所在!我从没想过这会影响这个账单组合框!注意副作用
我在上面添加了影响此组合的代码。它们在相同的表单上,不同的选项卡。用户类本身没有任何问题。@Ree您的问题在用户类中(在Visual Studio中右键单击FindBillers()
并选择转到定义(或突出显示代码并单击F12
)
- - (Inserted at 0)
Provider Default (Inserted at 1)
--
Provider 1
Provider 2
Provider 3