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

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

我还有一个在 TabControl 中有一个 DataGridView 的 Form 对象。在设计器中,我创建了一个 BindingSource 并使用添加项目数据源从顶级 LibraryData 对象创建一个源。然后,我将 DataGridView 绑定(bind)到设计器中的“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”

我在 Google 上广泛搜索过这个问题,并在 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
    }
  }
}

最佳答案

我认为问题在于,虽然您的表单上有绑定(bind)源,但您没有设置绑定(bind)源的数据源。

假设 bindingSource 是您放置在表单上的 datasource,然后在 MainForm_Load 中尝试以下操作:

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

关于c# - 如何将 BindingSource.DataSource 转换为自定义对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15766848/

相关文章:

c# - TraceSource 名称是如何在 .NET 程序中解析的?

c# - 异步任务数组。我可以将元数据附加到每个任务吗?

.net - dll 的 FileVersion 和 ProductVersion 之间有什么区别?

c# - 仅当使用 Process.Start() 在 C# 中作为外部程序运行时,C 控制台程序才会在退出时崩溃(访问冲突)

c# - HTTPListener 不在 .NET 4.6 和 Server2012R2 上使用 TLS 1.1/1.2

C# 将文本文件保存到 Excel

c# - 如何在 .NET 中进行应用程序内通信

.net - 有没有最小的 CLI 运行时的指针?

c# - 接口(interface)扩展 - 基类或扩展方法

c# - 使用 Json.NET 反序列化字符串和对象的混合集合