views:

39

answers:

1

I have data from a CD or Record currently setup like this:

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 = "";
    }

}

This works fine as I don't know how many tracks there are when I populate them so I can just add a new item to the list as each one is found.

However I can't work out how to get my Tracklist into the DataGridView without manually populating it cell by cell.

What would be a better way to structure the data?

Thanks in advance.

+1  A: 

Check out How to: Bind Objects to Windows Forms DataGridView Controls

If you don't want to manually set up the Tracklist field binding to the DataGridView control, you could set the AutoGenerateColumns property to True.

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

In the Track class, you might want to set up auto-implemented properties rather than public variables. For example,

class Track
{
    public string Position { get; set; } 
    public string Title { get; set; }
    public string TrackArtist { get; set; }
...
Lawrence P. Kelley
Worked a treat thanks!
Dave
Great! Glad to hear it. Would you select it as the accepted answer?
Lawrence P. Kelley