绑定+ ComBoBox选择问题 [英] Binding + ComBoBox Selection Issue
问题描述
LoadIDs函数将IDComboBox控件绑定到TestDataTable. Hi, The LoadIDs function binds the IDComboBox control to the TestDataTable. 应根据IDComboBox中的用户选择填充NameTextBox. The NameTextBox should be populated based on the user selection from IDComboBox. 但是,绑定IDComboBox时,它将使用第一个值作为IDComboBox.SelectedText,而不是"Select".并立即调用LoadName(). However, when IDComboBox is binding it uses the first value as IDComboBox.SelectedText rather than "Select". And it immediately calls LoadName(). 这是为什么?以及如何确保仅当用户选择有效项时才调用LoadName()? Why is this? And how do I ensure that LoadName() is called only when the user selects a valid item?
LoadName函数将NameTextBox绑定到NameDataTable
The LoadName function binds the NameTextBox to the NameDataTable
.
Thanks. private void LoadIDs()
{
// get ids
ListFrm.LoadID(thisID);
IDComboBox.DataSource = null;
IDComboBox.DataSource = ListFrm.TestDataTable;
IDComboBox.DisplayMember = "Test";
IDComboBox.ValueMember = "TestID";
IDComboBox.Text = "Select";
}
private void IDComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (IDComboBox.SelectedText != "Select")
{
LoadName();
}
}
private void LoadName()
{
ListFrm.LoadName(IDComboBox.Text);
NameTextBox.DataBindings.Clear();
NameTextBox.DataBindings.Add("Text", ListFrm.NameDataTable, "Name");
}
推荐答案
我的建议是
您应该在数据表中添加选择"行.并将其插入到DataTable的顶部,然后再将其绑定到ComboBox.
或者,您可以修改
My suggestion is that
You should add a row to the DataTable with "Select" and insert it at the top of the DataTable before binding it to the ComboBox.
Alternatively you can modify the
if
(IDComboBox.SelectedText != "Select"
)
{
LoadName();
}
if condition
Change it to
if
(IDComboBox.Text != "Select"
)
{
LoadName();
}
另外,我猜您正在对ComboBox使用DropDown样式.如果将其设置为DropDownList,则将无法将文本"设置为选择".
Also i guess you are using DropDown style to the ComboBox. If you set it to DropDownList then you will not be able to set the Text to "Select".
这篇关于绑定+ ComBoBox选择问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!