Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# IBindingList和IBindingListView_C#_List - Fatal编程技术网

C# IBindingList和IBindingListView

C# IBindingList和IBindingListView,c#,list,C#,List,怎么可能呢 List未实现IBindingList/IBindingListView——因此无法提供UI更新。您需要更改一些代码以适应BindigList api。UI通知通常通过事件完成。这意味着,在这种情况下,IBindingList和ListChanged事件;还有一些辅助接口,如IBindingListView,以及其他一些用于改进列表添加的可选功能等。BindingList几乎支持您所需的所有功能,但因此会产生一些小的开销 相比之下,List故意提供无此功能;它是一个通用(快速)动态列

怎么可能呢


List
未实现
IBindingList
/
IBindingListView
——因此无法提供UI更新。您需要更改一些代码以适应BindigList api。

UI通知通常通过事件完成。这意味着,在这种情况下,IBindingList和
ListChanged
事件;还有一些辅助接口,如IBindingListView,以及其他一些用于改进列表添加的可选功能等。
BindingList
几乎支持您所需的所有功能,但因此会产生一些小的开销

相比之下,
List
故意提供无此功能;它是一个通用(快速)动态列表设备,而不是“观察者”设备

注意:如果需要逐单元格更新,通常还需要键入
T
来实现INotifyPropertyChanged;如果不这样做,您只能获得行级更新(添加、删除等)

例如:

using System;
using System.ComponentModel;
using System.Windows.Forms;

static class Program
{
    class Foo
    {
        public int A { get; set; }
        public string B { get; set; }
    }
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        using (var form = new Form())
        using (var grid = new DataGridView { Dock = DockStyle.Fill })
        using (var add = new Button { Dock = DockStyle.Bottom, Text = "add" })
        using (var remove = new Button { Dock = DockStyle.Top, Text = "remove" })
        {
            form.Controls.Add(grid);
            form.Controls.Add(add);
            form.Controls.Add(remove);
            var lst = new BindingList<Foo>();
            var rnd = new Random();
            add.Click += delegate
            {
                lst.Add(new Foo { A = rnd.Next(1, 6), B = "new" });
            };
            remove.Click += delegate
            {
                int index = 0;
                foreach (var row in lst)
                { // just to illustrate removing a row by predicate
                    if (row.A == 2) { lst.RemoveAt(index); break; }
                    index++;
                }
            };
            grid.DataSource = lst;
            Application.Run(form);
        }
    }
}
使用系统;
使用系统组件模型;
使用System.Windows.Forms;
静态类程序
{
福班
{
公共int A{get;set;}
公共字符串B{get;set;}
}
[状态线程]
静态void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
使用(var form=new form())
使用(var grid=newdatagridview{Dock=DockStyle.Fill})
使用(var add=new按钮{Dock=DockStyle.Bottom,Text=“add”})
使用(var remove=new按钮{Dock=DockStyle.Top,Text=“remove”})
{
表单.控件.添加(网格);
form.Controls.Add(添加);
窗体.控件.添加(删除);
var lst=新绑定列表();
var rnd=新随机数();
添加。单击+=委派
{
lst.Add(newfoo{A=rnd.Next(1,6),B=“new”});
};
删除。单击+=委派
{
int指数=0;
foreach(lst中的var行)
{//仅说明如何按谓词删除行
如果(row.A==2){lst.RemoveAt(index);break;}
索引++;
}
};
grid.DataSource=lst;
申请表格;
}
}
}

Btw,如果我写了“foray”,那就意味着我的iPod拼写很糟糕,如果列表是连接dataGridViwe的话?在更新(删除,添加,…)列表之后,dataGridViwe也将更新。@mah_85在这种情况下,bindinglist将引发事件来告诉UI发生了什么,UI将更新<代码>列表不会公开此类事件,因此UI无法自动更新自身。非常感谢。请以windows窗体应用程序的形式编写代码。感谢you@mah_85-即windows窗体应用程序;
使用System.Windows.Forms是一个bug线索。出于示例目的,这里的任何示例都是故意最小化的。但同样的方法也适用于常规代码。这个小示例非常出色,它可以编写得非常紧凑,非常感谢。