paw*_*wel 2 c# combobox winforms
我有一个包含Foo类型对象的组合框,这是该类Foo:
public class Foo
{
public string name { get; set; }
public string path { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
是Foo.name组合框中显示的文本,Foo.path也是所选选项的值。
我想在进行一些操作后从组合框中删除一个选项。
我尝试过这些方法:
1
comboBox2.Items.Remove(@comboBox2.Text);
Run Code Online (Sandbox Code Playgroud)2
comboBox2.Items.Remove(@comboBox2.SelectedValue.ToString());
Run Code Online (Sandbox Code Playgroud)3
Foo ToDelete = new Foo();
ToDelete.name = @comboBox2.Text;
ToDelete.path = @comboBox2.SelectedValue.ToString();
comboBox2.Items.Remove(ToDelete);
Run Code Online (Sandbox Code Playgroud)没有什么对我有用。: / 这个怎么做?
更新
这就是我初始化组合框的方式:
string[] filePaths = Directory.GetFiles(sites.paths[comboBox1.SelectedIndex]);
List<Foo> combo2data = new List<Foo>();
foreach (string s in filePaths)
{
Foo fileInsert = new Foo();
fileInsert.path = s;
fileInsert.name = Path.GetFileName(s);
combo2data.Add(fileInsert);
}
comboBox2.DataSource = combo2data;
comboBox2.ValueMember = "path";
comboBox2.DisplayMember = "name";
Run Code Online (Sandbox Code Playgroud)
comboBox2.Items.Remove(comboBox2.SelectedValue); 只会从组合框中删除,而不是从绑定到组合框的数据源中删除。您可以将其从数据源中删除并重新绑定数据源。