开发者

copy bindinglist to another one

开发者 https://www.devze.com 2023-02-14 05:53 出处:网络
public class MyBindingList : BindingList<int> { public MyBindingList() { } private BindingList<int> temp;
public class MyBindingList : BindingList<int>
        {
            public MyBindingList()
            {
            }
            private BindingList<int> temp;
            public void Backup()
            {
                int[] arr = new int[this.Count];
                this.CopyTo(arr,0);
                temp = new BindingList<int>(arr);
            }
            public void Restore()
            {
                this.Items.Clear();
                //for(int i=0;i<temp.Count;i++) this.Add(temp[i]);
            }
   开发者_JAVA技巧     }

//for(int i=0;i<temp.Count;i++) this.Add(temp[i]);

is a so slowly way to restore, so what can i use for more efficiently restore()?


With your example, foreach is the easiest and quickest way.

The faster way is via a copy-contructor. However with your example, it won't work. You can't say this = new ...

myList = new BindingList<int>(temp);

Edit: Side-comment, you can clean up Backup() by deleting the creation of the array and just call:

public void Backup()
{
    this.temp = new BindingList<int>(this);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号