我有BindingSource一个BindingList<Foo>附件作为数据源。我想使用BindingSource'sFind方法来查找元素。但是,NotSupportedException当我执行以下操作时会抛出 a ,即使我的数据源确实实现了IBindingList(并且 MSDN 中没有记录此类异常):
int pos = bindingSource1.Find("Bar", 5);
我在下面附上了一个简短的例子(a ListBox、aButton和 a BindingSource)。任何人都可以帮助我使Find调用正常工作吗?
public partial class Form1 : Form
{
public Form1() { InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e)
{
var src = new BindingList<Foo>();
for(int i = 0; i < 10; i++)
{
src.Add(new Foo(i));
}
bindingSource1.DataSource = src;
}
private void button1_Click(object sender, EventArgs e)
{
int pos = bindingSource1.Find("Bar", 5);
}
}
public sealed class Foo
{
public Foo(int bar)
{
this.Bar = bar;
}
public int Bar { get; private set; }
public override string ToString()
{
return this.Bar.ToString();
}
}