列表未显示在绑定源中

时间:2018-06-28 06:41:27

标签: c# winforms entity-framework ef-code-first

我的数据库中有以下集合:

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

但是,当我将客户数据源绑定到我的datagridview时,我无法获得药物清单,该怎么做?

1 个答案:

答案 0 :(得分:0)

您可以通过执行以下操作来实现此目的:

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

请尝试一下,告诉我是否可行。

编辑:我在我制作的程序中使用了它:

//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;

但是我是从xlsm文件中获取值的,也许您可​​以为此添加代码并使其起作用。但是我认为这可能是您前进的一种方式。

相关问题