如何将BindingSource.DataSource转换为我的自定义对象?

时间:2013-04-02 14:32:35

标签: c# .net

我将自定义数据集样式类定义为:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace LibrarySort.Data
{
  public class LibraryData
  {
    private AlbumList albums;
    public AlbumList Albums { get { return albums; } }

    public LibraryData()
    {
      this.albums = new AlbumList();
      this.albums.AllowEdit = true;
      this.Albums.AllowNew = true;
      this.Albums.AllowRemove = true;
    }

    public void FillAll()
    {
      this.Albums.Fill();
    }
  }

  public class AlbumList : BindingList<Album>
  {
    public AlbumList()
    {
    }

    public void Fill()
    {
      int id = 1;
      Album album1 = new Album();
      album1.Id = id++;
      album1.Artist = "Classical Piano Artist";
      album1.Download = true;
      album1.Person = null;
      album1.Price = (decimal?)3.49;
      album1.Tags.Add("classical");
      album1.Tags.Add("piano");
      album1.Title = "Classical Piano";
      Album album2 = new Album();
      album2.Id = id++;
      album2.Artist = "Thrash Metal Artist";
      album2.Download = false;
      album2.Person = null;
      album2.Price = (decimal?)7.99;
      album2.Tags.Add("thrash metal");
      album2.Title = "Thrash Metal";

      this.Items.Add(album1);
      this.Items.Add(album2);
    }
  }
}

我还有一个Form对象,在TabControl里面有一个DataGridView。在设计器中,我创建了一个BindingSource,并使用Add Project Data Source从顶级LibraryData对象创建一个源。然后我将DataGridView绑定到设计器中的“Albums”数据成员,并按照预期在设计器中填充列。

运行代码时,表格未填充,这是有意义的,因为Fill()尚未运行。所以我为表单创建一个Load事件处理程序,如下所示:

private void MainForm_Load(object sender, EventArgs e)
{
  LibraryData data = (LibraryData)bindingSource.DataSource;
  data.FillAll();
}

但是在运行中,我在MainForm_Load()中得到以下内容:

System.InvalidCastException未处理 Message =“无法将'System.RuntimeType'类型的对象强制转换为'LibrarySort.Data.LibraryData'

我已经广泛搜索了这个,并且在StackOverflow中但没有运气。我错过了什么吗?

更新:DataSource肯定是非空的。同样有趣的是,在设计师代码中我看到了:

this.bindingSource.DataSource = typeof(LibrarySort.Data.LibraryData);

更新2:专辑类和父项:

namespace LibrarySort.Data
{
  public class Album : Item
  {
    bool download = false;
    public bool Download { get { return download; } set { download = value; } }

    string artist = null;
    public string Artist { get { return artist; } set { artist = value; } }
  }
}

namespace LibrarySort.Data
{
  public class Item
  {
    int id = -1;
    public int Id { get { return id; } set { id = value; } }

    // FK to Person that currently has possession of item
    int? person = null;
    public int? Person { get { return person; } set { person = value; } }

    string title = null;
    public string Title { get { return title; } set { title = value; } }

    decimal? price = null;
    public decimal? Price { get { return price; } set { price = value; } }

    State state = State.Owned;
    public State State { get { return state; } set { state = value; } }

    List<string> tags = null;
    public List<string> Tags
    {
      get
      {
        if (tags == null)
          tags = new List<string>();
        return tags;
      }
      // No set needed
    }
  }
}

1 个答案:

答案 0 :(得分:1)

我认为问题是虽然您的表单上有绑定源,但您尚未设置绑定源的数据源。

假设bindingSource是您在表单上删除的datasource,请在MainForm_Load中尝试以下内容:

LibraryData data = new LibraryData();
data.FillAll();
bindingSource.DataSource = data;