C# 如何将BindingSource.DataSource转换为自定义对象?

C# 如何将BindingSource.DataSource转换为自定义对象?,c#,.net,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 Albu

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

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);
    }
  }
}
但是,在运行时,我在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
    }
  }
}
namespace LibrarySort.Data
{
公共类相册:项目
{
bool下载=false;
公共bool下载{get{return Download;}set{Download=value;}}
字符串艺术家=null;
公共字符串艺术家{get{return Artist;}set{Artist=value;}}
}
}
命名空间LibrarySort.Data
{
公共类项目
{
int id=-1;
公共int Id{get{return Id;}set{Id=value;}}
//FK给当前拥有物品的人
int?person=null;
公共int?Person{get{return Person;}set{Person=value;}}
字符串标题=null;
公共字符串标题{get{return Title;}set{Title=value;}}
十进制?价格=空;
公共十进制?价格{get{return Price;}set{Price=value;}}
国有=国有;
公共状态状态{get{return State;}set{State=value;}}
列表标签=null;
公共列表标签
{
得到
{
if(标记==null)
标签=新列表();
返回标签;
}
//不需要设置
}
}
}

我认为问题在于,尽管表单上有绑定源,但尚未设置绑定源的数据源

假设
bindingSource
是您在表单上删除的
datasource
,然后在
main表单加载中尝试以下操作:

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

你遇到过这个例子吗?@Derek我确实看到过这个例子,但它假设你只需要一个列表。在我的项目中,我将有几个列表,其中两个列表具有父/子关系,因此我创建了一个父对象来包含所有列表。你能发布Album类吗?我假设Album有一个名为Tags的属性,它是一个列表对象Object还?您的数据源设置错误。
this.bindingSource.DataSource=typeof(LibrarySort.data.LibraryData);
正在将您的数据源设置为
系统。键入
实例,而不是
LibraryData
实例。@MichaelEdenfield理解,但设置该代码的是设计师。我在设计师中做了什么错事吗?我尝试了这个方法,但效果很好,但这意味着设计师没有正确地选择列。在t中,一切看起来都很好但是他没有UI。如果没有设计师风格的解决方案,我将使用此解决方案鉴于上面@MichaelEdenfield的评论,这似乎是正确的解决方案。谢谢
LibraryData data = new LibraryData();
data.FillAll();
bindingSource.DataSource = data;