将派生类添加到DataGridView的绑定源 [英] Adding derived classes to a Binding Source of a DataGridView

查看:84
本文介绍了将派生类添加到DataGridView的绑定源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WinForms中使用C#使用MVC模式

Using MVC Pattern in WinForms with C#

三个业务对象-一个抽象的LibraryItem类,一个Book类和一个日记类,均从LibraryItem派生

Three business object - an abstract LibraryItem Class ,a Book class and a journal class , both derives from LibraryItem

//LibraryItem Class
public abstract class LibraryItem
{
    public string Title { get; set; }

    private static int _nextCallNum = 1000;

    public int CallNum { get; private set; }

    public int NumCopies { get; set; }
}

//Journal Class
 public class Journal : LibraryItem
{
    public int Vol { get; set; }
}

//Book Class
 public class Book : LibraryItem
{
    public string Author { get; set; }
}


//LibraryController Class 
  public class LibrayController
{

    //a List that can hold both books and journals
    private List<LibraryItem> items;

     //adds a book object to items
     public void AddBookItem(string title, int numCopies , string author) {.....}

      //adds a journal object to items
     public void AddJournal(string title, int vol, int numCopies){}

      //returns a List contaning books and journals to the View
      public List<LibraryItem> GetAllItems()
    {
        return items;
    }
}   

在Win Forms中,使用DataGridView显示LibraryItems的列表

In Win Forms , using a DataGridView to display list of LibraryItems

dataGridViewItems.DataSource = null;

booksbindingSource = new BindingSource();

 foreach (LibraryItem anitem in controller.GetAllItems())
        {

            LibraryItem aLibraryItem = (LibraryItem) anitem;

            booksbindingSource.Add(aLibraryItem);
        }

dataGridViewItems.DataSource = booksbindingSource;

当在日记本之后添加一本书,或在绑定源中添加本书之后的日记时,我得到 InvalidOperationException添加到BindingSource列表中的对象必须都是同一类型.

When adding a book after journal, or a journal after book to the binding source, I get InvalidOperationException Objects added to a BindingSource's list must all be of the same type.

说明是不言自明的.我需要一种在数据网格中显示LibraryItems的方法,而不是书本和日记本.我只需要在数据网格中显示基类属性(Title,CallNum和NumCopies of LibraryItem).

The description is self-explanatory. I need a way to display LibraryItems in a datagrid than can be both books and journals. I only need to display the base class properties(Title, CallNum and NumCopies of LibraryItem) in the datagrid.

推荐答案

如果尚未设置BindingSourceDataSource属性,则添加到列表中的第一个对象将定义列表的类型,然后您要添加到列表中的所有项目都应该是第一个对象的类型,因为内部列表必须包含

If the DataSource property of BindingSource has not already been set, then the first object added to the list defines the type for the list, and then all items that you want to add to the list, should be of the type of first object, because the internal list must contain homogeneous types as mentioned in documentations.

代替使用Add方法将项目添加到BindingSource的方法,将其DataSource属性设置为您从控制器获取的列表:

Instead of adding items to BindingSource using Add method, set its DataSource property to the list which you get from controller:

var bs = new BindingSource();
var list = controller.GetAllItems();
bs.DataSource = list;
this.dataGridView1.DataSource = bs;

注意

要解决此问题,其足够了,建议使用上述方法,但要查看设置DataSource的影响,可以将DataSource属性设置为typeof(LibraryItem),您将看到它也可以正常使用:

To solve the problem, its enough and recommended to use above method, but to see the affect of setting DataSource, you can set DataSource property to typeof(LibraryItem) and you will see it also works as expected:

bs.DataSource = typeof(LibraryItem);
foreach (var item in list)
{
    bs.Add(item);
}
this.dataGridView1.DataSource = bs;

这篇关于将派生类添加到DataGridView的绑定源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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