gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:54:45 24 4
gpt4 key购买 nike

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

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com