简体   繁体   English

如何将列表计数绑定到标签

[英]How to bind a list count to a label

I have a DataGridView binding to a list and a label showing number of records. 我有一个绑定到列表的DataGridView和一个显示记录数的标签。 I run into the same problem Khash had. 我遇到了Khash遇到的相同问题。 (so I steal his title). (所以我偷了他的头衔)。 Any add or delete operations on the grid won't update the label. 网格上的任何添加或删除操作都不会更新标签。

在此处输入图片说明

Based on Sung's answer, a facade wrapper , I create my custom list inheriting BindingList and implementing INotifyPropertyChanged . 基于Sung的答案,即外观包装 ,我创建了继承BindingList并实现INotifyPropertyChanged自定义列表。

public class CountList<T> : BindingList<T>, INotifyPropertyChanged
{    
    protected override void InsertItem(int index, T item)
    {
        base.InsertItem(index, item);
        OnPropertyChanged("Count");
    }

    protected override void RemoveItem(int index)
    {
        base.RemoveItem(index);
        OnPropertyChanged("Count");
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

However, this will throw an exception when binding. 但是,这将在绑定时引发异常。

Cannot bind to the property or column Count on the DataSource. Parameter name: dataMember

Below is my binding code: 以下是我的绑定代码:

private CountList<Person> _list;

private void Form1_Load(object sender, EventArgs e)
{
    _list = new CountList<Person>();
    var binding = new Binding("Text", _list, "Count");
    binding.Format += (sender2, e2) => e2.Value = string.Format("{0} items", e2.Value);
    label1.DataBindings.Add(binding);
    dataGridView1.DataSource = _list;
}

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
}

Any suggestions will be appreciated. 任何建议将不胜感激。 Thank you. 谢谢。

In fact, it is much simpler than you think! 实际上,它比您想象的要简单得多!

Microsoft already has created the BindingSource control, so, you need to use it and then handle the BindingSource event to update the label: Microsoft已经创建了BindingSource控件,因此,您需要使用它,然后处理BindingSource事件以更新标签:

    public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    private BindingSource source = new BindingSource();

    private void Form1_Load(object sender, EventArgs e)
    {
        var items = new List<Person>();
        items.Add(new Person() { Id = 1, Name = "Gabriel" });
        items.Add(new Person() { Id = 2, Name = "John" });
        items.Add(new Person() { Id = 3, Name = "Mike" });
        source.DataSource = items;
        gridControl.DataSource = source;
        source.ListChanged += source_ListChanged;

    }

    void source_ListChanged(object sender, ListChangedEventArgs e)
    {
        label1.Text = String.Format("{0} items", source.List.Count);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM