1

I have the following collection in my database:

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Medication> Medications { get; set; }
}

public class Medication
{
    public int Id { get; set; }
    public string Name { get; set; }
}

However when I bind the Customer data source to my datagridview, I can not get the list of medications, what can I do to get that list?

Kuseq
  • 11
  • 1

1 Answers1

0

You can probably achieve this by doing:

 List<Medication> medications = new List<Medication>();
        var source = new BindingSource();
        source.DataSource = Medications;
        dataGridView1.DataSource = source;

Please try it and tell me if it works.

EDIT: I used this in a program I made:

//My list
List<UA> listagem = new List<UA>();
//My class
public class UA
    {
        public string DistritoCod { get; set; }
        public string Distrito { get; set; }
        public UA() { }
    }

//Then this to tell where I wanted the values to go
   UA ua = new UA { };
            ua.DistritoCod = row.Cell(1).GetString();
            ua.Distrito = row.Cell(2).GetString();

listagem.Add(ua);

datagridview1.datasource = listagem;

But I was getting my values from a xlsm file, maybe you can addapt your code to this and make it work. But I think this can be a way fo you to move forward.

lpereira
  • 11
  • 7
  • source.DataSource = Medications; it says Medications is a type which is not valid in the given context – Kuseq Jun 28 '18 at 09:26
  • Are you getting your list values from a excel or xlsm file? – lpereira Jun 28 '18 at 09:29
  • From a localdb, Im using ef code first on a winform – Kuseq Jun 28 '18 at 09:32
  • Try adapt your code to that, I'm sorry if I'm not able to help you. This worked for me but as you are using a localdb I honestly don't know if this can give the help you need. – lpereira Jun 28 '18 at 09:44