我正在使用Windows窗体应用程序..
我有两个组合框..我将我的组合框drop down style
属性更改为DropDownList
保存数据后我想清除组合框中的项目..所以我给出了这样的代码:
CmbDepartment.Text = "";
cmbvisitpurpose.Text = "";
但这并没有从我的组合框中清除所选项目。所以我改变了这样的代码:
cmbvisitpurpose.Items.RemoveAt(cmbvisitpurpose.SelectedIndex = -1)
CmbDepartment.Items.RemoveAt(CmbDepartment.SelectedIndex = -1)
这是从我的组合框中永久删除特定项目..如果我想获取combbox中的所有项目..我要加载页面...我想要清除所选项目.. 我怎么能这样做?
答案 0 :(得分:1)
这只会从组合框中删除,而不是从数据源中删除。
如果您想保留这些项目,请更好地使用本地收藏。
CmbDepartment.Items.Remove(CmbDepartment.SelectedItem);
以下是如何将值分配给集合的示例
List<string> DepartmentsPermanent;
List<string> DepartmentsTemporary;
public Form1()
{
InitializeComponent();
DepartmentsPermanent = new List<string>();
DepartmentsPermanent.Add("EEE");
DepartmentsPermanent.Add("CSE");
DepartmentsPermanent.Add("E&I");
DepartmentsPermanent.Add("Mechanical");
comboBox1.DataSource = DepartmentsPermanent;
//here you assign the values to other List
DepartmentsTemporary = DepartmentsPermanent.ToList();
}
//Now if you have selected EEE from the list and you want to remove on selection
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedItem != null && DepartmentsTemporary != null)
{
DepartmentsTemporary.Remove(comboBox1.SelectedItem.ToString());
comboBox1.DataSource = DepartmentsTemporary;
}
//If you want to assign the default values again you can just assign the PermanentList
//comboBox1.DataSource = DepartmentsPermanent;
}
答案 1 :(得分:0)
如果要清除所选项目,则应将ComboBox.SelectedIndex
属性设置为-1