我有一些标签应该显示包含绑定到DataGridView的BindingList的实际项目数量。
我试图以这种方式绑定:
CountOfLoadedItemsLabel.DataBindings.Add("Text", _items.Count, String.Empty);
但是当BindingList更新时,绑定到其Count属性的标签不会改变。
答案 0 :(得分:0)
从未使用过BindingList< T>但这对我有用:
public partial class Form1 : Form
{
private BindingList<Test> list;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.list = new BindingList<Test>
{
new Test(1,"Entry"),
new Test(2,"Another Entry")
};
dataGridView1.DataSource = new BindingSource(list,null);
list.ListChanged += list_ListChanged;
list.Add(new Test(3, "After Binding"));
}
void list_ListChanged(object sender, ListChangedEventArgs e)
{
CountOfLoadedItemsLabel.Text = string.Format("Items: {0}", list.Count);
}
}
public class Test
{
public int Id { get; set; }
public string Name { get; set; }
public Test(int id, string name)
{
this.Id = id;
this.Name = name;
}
}