【发布时间】:2012-06-13 14:12:48
【问题描述】:
我有一个BindingSource 附加一个BindingList<Foo> 作为数据源。我想使用BindingSource 的Find method 来查找元素。但是,当我执行以下操作时,会抛出 NotSupportedException,即使我的数据源确实实现了 IBindingList(并且 MSDN 中没有记录此类异常):
int pos = bindingSource1.Find("Bar", 5);
我在下面附上了一个简短的示例(ListBox、Button 和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();
}
}
【问题讨论】:
标签: c# .net winforms data-binding bindingsource