4

我有一个自定义数据集样式类定义为:

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);
    }
  }
}

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

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

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

但是在运行时,我在 MainForm_Load() 中得到以下信息:

System.InvalidCastException 未处理 Message="Unable to cast object of type 'System.RuntimeType' to type '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
    }
  }
}
4

1 回答 1

2

我认为问题在于,尽管您的表单上有绑定源,但您还没有设置绑定源的数据源。

假设这bindingSourcedatasource您在表单上放置的,然后尝试以下操作MainForm_Load

LibraryData data = new LibraryData();
data.FillAll();
bindingSource.DataSource = data;
于 2013-04-02T14:58:26.260 回答