如何在dataGrid.ItemsSource中包含ICollection <continent>时返回一个字符串

时间:2017-06-10 22:39:34

标签: wpf datagrid datagridviewcolumn icollection

当我显示我的数据库表时,它包含一些集合,而那些不会显示在数据网格中。生成一列但它保持空白。我不知道在哪里实现这一点。

也许这也有助于解释我想要在列中显示的内容而不是ICollection

static void Main(string[] args)
    {
        using (var db = new Whataboutthisfish())
        {
            Vis vis = db.Vissen.Find(2);
            if(vis != null)
            {                    
                if(vis.Continenten != null)
                {
                    string s = "";
                    if(vis.Continenten.Count() > 1)
                    {
                        var continentenLijst = vis.Continenten;

                        s = continentenLijst.First().Naam;
                        foreach (Continent c in vis.Continenten)
                        {
                            s += ", "+c.Naam;
                        }                            
                    }
                    else
                    {
                        s = vis.Continenten.First().Naam;
                    }                    
                    Console.WriteLine(s);
                }
            }
        }
    }

我想使用分隔符返回包含每个大陆名称的1个字符串。

赞:&#34;北美&#34;如果集合中只有一个大陆 或者:&#34;北美,南美&#34;等多个。

Classes;

[Table("Continenten")]
public partial class Continent
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Continent()
    {
        Vissen = new HashSet<Vis>();
    }

    public int Id { get; set; }

    [Required]
    [StringLength(50)]
    public string Naam { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Vis> Vissen { get; set; }
}

}

[Table("Vissen")]
public partial class Vis
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Vis()
    {
        Continenten = new HashSet<Continent>();
        Verbanden = new HashSet<Verband>();
        Waterlagen = new HashSet<Waterlaag>();
    }

    public int Id { get; set; }

    [StringLength(200)]
    public string Naam { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Continent> Continenten { get; set; }
}

网格的Xaml:

        <DataGrid x:Name="dataGrid" HorizontalAlignment="Left" VerticalAlignment="Top" RenderTransformOrigin="0.833,0.846" Margin="10,10,10,10"/>

在代码背后:

using (var db = new Catattafish.Whataboutthisfish())
        {               
            dataGrid.ItemsSource = db.Vissen.ToList(); 
        }

0 个答案:

没有答案