DataFormat for DataGrid的DataSource

时间:2009-11-30 18:01:09

标签: c# data-structures datagridview

我的CD或记录中的数据目前设置如下:

class Release       //class containing all details of release
{
    public string Artist;       //Release Artist
    public string Title;        //Release Title
    public string Released;     //Released (Date)
    public Image Image;         //Image

    private List<Track> trackList = new List<Track>();    //List containing tracks
    public List<Track> TrackList
    {
        get
        {
            return trackList;
        }
    }

    //CONSTRUCTOR
    public Release()
    {
        //Initialize strings to empty strings
        Artist = "";
        Title = "";
        Released = "";
        Image = null;
    }

class Track
{
    public string Position;
    public string Title;
    public string TrackArtist;

    public Track()
    {
        //Initialize strings
        Position = "";
        Title = "";
        TrackArtist = "";
    }

}

这很好用,因为当我填充它们时我不知道有多少曲目所以我可以在列表中添加一个新项目,因为每个都找到了。

但是我无法弄清楚如何将我的Tracklist放入DataGridView,而无需逐个单元手动填充它。

构建数据的更好方法是什么?

提前致谢。

1 个答案:

答案 0 :(得分:1)

查看How to: Bind Objects to Windows Forms DataGridView Controls

如果您不想手动设置Tracklist字段绑定到DataGridView控件,可以将AutoGenerateColumns属性设置为True。

DataGridView1.AutoGenerateColumns = True
DataGridView1.DataSource = TrackList
...

在Track类中,您可能希望设置自动实现的属性而不是公共变量。例如,

class Track
{
    public string Position { get; set; } 
    public string Title { get; set; }
    public string TrackArtist { get; set; }
...