我有一个组合框,其中iam使用以下代码加载组合框。
public void LoadCustomer1(ComboBox pCmbCustomer)
{
obj._dtInputParameter.Clear();
obj.AddInputParameter("@Prm_OpFlag", "S", "String", 1);
//obj.strSPName = "prc_CUST_Details";
obj.strSPName = "EditCustCombo";
DataSet ds = obj.SqlExecuteFill();
pCmbCustomer.DataSource = ds.Tables[0];
pCmbCustomer.DisplayMember = "CustomerId";
pCmbCustomer.ValueMember = "CustomerId";
pCmbCustomer.Text = "--- Select Customer Id ---";
pCmbCustomer.SelectedIndex = 0;
}
问题在于pCmbCustomer.DataSource = ds.Tables [0];选择了combobox的indexchanged事件正在工作。我可以在绑定组合框时避免选择indexchanged事件吗?有人可以帮忙吗?
答案 0 :(得分:1)
完成对组合框的绑定后,您可以附加到SelectedIndexChanged事件处理程序。
因此,不要将事件直接附加到用户控件中,而是在LoadCustomer1结尾处或在调用LoadCustomer1之后将其附加到代码隐藏中。
答案 1 :(得分:1)
尽量避免处理selectedIndexChangedEvent
。
避免使用pCmbCustomer.Text = "--- Select Customer Id ---";
这种类型的陈述。也就是说,不要明确设置文本。
将文本"--- Select Customer Id ---"
作为列表成员。
然后在需要时使用此pCmbCustomer.SelectedIndex = 0,2,3...,n;
语句。
答案 2 :(得分:1)
如果可能,我会使用SelectionChangeCommitted而不是使用SelectedIndexChange。
MSDN:http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectionchangecommitted.aspx
“仅当用户更改组合框选择时才会引发SelectionChangeCommitted。不要使用SelectedIndexChanged或SelectedValueChanged来捕获用户更改,因为当选择以编程方式更改时也会引发这些事件。”