[英]How to bind BindingList<T>.Count property to specific label text?
I have some label that should display actual amount of items that contain BindingList that bound to the DataGridView. 我有一些标签应该显示包含绑定到DataGridView的BindingList的项目的实际数量。
I tried to bind in this way: 我试图以这种方式绑定:
CountOfLoadedItemsLabel.DataBindings.Add("Text", _items.Count, String.Empty);
But when BindingList updates, the label that bound to its Count property not changes. 但是,当BindingList更新时,绑定到其Count属性的标签不会更改。
Never used BindingList<T> but this worked for me: 从未使用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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.