简体   繁体   中英

How do I convert BindingSource.DataSource to my custom object?

I have a custom dataset style class defined as:

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

I also a have Form object that inside a TabControl has a DataGridView. In the designer, I created a BindingSource and used the Add Project Data Source to create a source from the top level LibraryData object. I then bind the DataGridView to the "Albums" data member, all in the designer, and the columns get populated in the designer as expected.

When running the code, the table isn't populated, which makes sense as the Fill() hasn't been run. So I create a Load event handler for the form as follows:

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

However on run I get the following in MainForm_Load():

System.InvalidCastException was unhandled Message="Unable to cast object of type 'System.RuntimeType' to type 'LibrarySort.Data.LibraryData'

I've Googled extensively on this, and in StackOverflow but no luck. Am I missing something?

Update: DataSource is definitely non-null. Also interestingly, in the designer code I see this:

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

Update 2: Album class and parent Item:

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

I think the issue is that although you have a binding source on your form, you have not set the datasource of the binding source.

Assuming that bindingSource is the datasource you dropped on your form, then try the following in the MainForm_Load :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM