c#基于IList更新Datagridview [英] c# Update Datagridview based on IList

查看:212
本文介绍了c#基于IList更新Datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的类:People:

I have a very simple class: People:

class People
{
   private string LastName = null;
   private string FirstName = null;
   private string Status = null;

  public string lastName
  {
     get { return LastName; }
     set { LastName = value; }
  }

  public string firstName
  {
     get { return FirstName; }
     set { FirstName = value; }
  }

  public string status
  {
     get { return Status; }
     set { Status = value; }
  }

  public People(string lastName, string firstName, string status)
  {
     LastName = lastName;
     FirstName = firstName;
     Status = status;
  }
}

而且,我有另一个类实现了IList< ;>,这意味着用作People类的集合,称为PeopleList<> ;. PeopleList<>是特殊的,因为我直接绑定这个类的一个实例到DataGridView。通常,由于一个重要的原因,我永远不会使用IList<>作为datagridview(DGV)的数据源:IList不会生成ListChanged事件,这意味着一旦将DGV绑定到IList<>行数在DGV中设置。换句话说,如果将新项目添加到IList<>,则DGV将不显示它们!要使其工作的唯一方法是将DGV的数据源设置为null,然后在更改后将其重新绑定到IList<>。

And, I have another class that implements the interface IList<>, which is meant to be used as a collection of the People class, called PeopleList<>. PeopleList<> is special because I am directly binding an instance of this class to a DataGridView. Normally, I would never use an IList<> as a datasource for a datagridview (DGV) for one important reason: an IList does not generate the ListChanged event, which means that once you bind the DGV to the IList<> the number of rows in the DGV is set. In other words, if new items are added to the IList<>, the DGV will not show them! The only way to get that to work is to set the datasource of the DGV to null, and then rebind it to the IList<> after the change is made.

BindingList更适合这种类型的需求,但是唉,由于我不能去的原因,我使用IList< ;>作为接口。为了解决这个问题,我发现了一些代码,应该将ListChanged事件集成到IList<>接口的实现,但我有一些麻烦。它似乎甚至与这个代码我必须有一个事件处理程序方法吗?在某些时候,我必须声明这个方法作为ListChanged事件的处理程序?看看:

A BindingList is much more suited to this type of need, but alas, for reasons I can't go into, it is imperative that I use an IList<> as the interface. To get around this problem, I have found some code that supposedly integrates the ListChanged event into an implementation of the IList<> interface, but I am having some trouble with it. It seems like even with this code I would have to have an event handler method right? And at some point I would have to declare this method as the handler for the ListChanged event? Have a look:

class PeopleList<T> : IList<T>
   {
       private IList<T> internalList;

    public class ListChangedEventArgs : EventArgs {
      public int index;
      public T item;
      public ListChangedEventArgs(int index, T item) {
        this.index = index;
        this.item = item;
      }
    }

    public delegate void ListChangedEventHandler(object source, ListChangedEventArgs e);
    public delegate void ListClearedEventHandler(object source, EventArgs e);
    public event ListChangedEventHandler ListChanged;
    public event ListClearedEventHandler ListCleared;

    public PeopleList() {
      internalList = new List<T>();
    }

    public PeopleList(IList<T> list) {
      internalList = list;
    }



    public PeopleList(IEnumerable<T> collection) {
      internalList = new List<T>(collection);
    }

    protected virtual void OnListChanged(ListChangedEventArgs e) {
      if (ListChanged != null)
        ListChanged(this, e);
    }

    protected virtual void OnListCleared(EventArgs e) {
      if (ListCleared != null)
        ListCleared(this, e);
    }

    public int IndexOf(T item) {
      return internalList.IndexOf(item);
    }

    public void Insert(int index, T item) {
      internalList.Insert(index, item);
      OnListChanged(new ListChangedEventArgs(index, item));
    }

    public void RemoveAt(int index) {
      T item = internalList[index];
      internalList.Remove(item);
      OnListChanged(new ListChangedEventArgs(index, item));
    }

    T this[int index] {
      get { return internalList[index]; }
      set {
            internalList[index] = value;
            OnListChanged(new ListChangedEventArgs(index, value));
      }
    }

    public void Add(T item) {
      internalList.Add(item);
      OnListChanged(new ListChangedEventArgs(internalList.IndexOf(item), item));
    }

    public void Clear() {
      internalList.Clear();
      OnListCleared(new EventArgs());
    }

    public bool Contains(T item) {
      return internalList.Contains(item);
    }

    public void CopyTo(T[] array, int arrayIndex) {
      internalList.CopyTo(array, arrayIndex);
    }

    public int Count {
      get { return internalList.Count; }
    }

    public bool IsReadOnly {
      get { return IsReadOnly; }
    }

    public bool Remove(T item) {
      lock(this) {
        int index = internalList.IndexOf(item);
        if (internalList.Remove(item)) {
          OnListChanged(new ListChangedEventArgs(index, item));
          return true;
        }
        else
          return false;
      }
    }

    public IEnumerator<T> GetEnumerator() {
      return internalList.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator() {
      return ((IEnumerable) internalList).GetEnumerator();
    }


    T IList<T>.this[int index]
    {
       get
       {
          throw new NotImplementedException();
       }
       set
       {
          throw new NotImplementedException();
       }
    }

}

FYI - 我在VS2010设计。

FYI - I am designing in VS2010.

任何指导都将非常感谢...谢谢!

Any guidance would be greatly appreciated... Thanks!

推荐答案

好吧,所以我没有听到回来,似乎没有办法使这项工作没有严重的副作用。我最后只是删除我的代码,并将我的列表类基于System.ComponentModel.BindingList<>这似乎是工作确定。我知道这是一个可能性(正如我在我的问题中提到的),但希望避免改变基类的繁琐劳动,因为我不是编写原始代码的人。哦井。 =)

Ok, so I didn't hear back, and there doesn't appear to be a way to make this work without serious side effects. I ended up just gutting my code and basing my list class on System.ComponentModel.BindingList<> which seems to be working ok. I knew that this was a possibility (as I mentioned in my question) but was hoping to avoid the tedious labor of changing out the base class as I wasn't the one who wrote the original code. Oh wells. =)

这篇关于c#基于IList更新Datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆