绑定对象的DataGridView C# [英] Binding objects DataGridView C#

查看:140
本文介绍了绑定对象的DataGridView C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataGridView,而我想展现对象的列表。

的对象是这些:

 公共类实体
{
    公众诠释ID {搞定;组; }
}公共类旅游:实体
{
    公共服务服务{搞定;组; }
    公共市源{搞定;组; }
    众城命运{搞定;组; }
    公共十进制价格{搞定;组; }
}公共类服务:实体
{
    公共字符串名称{;组; }
}公共类城市:实体
{
    公共字符串名称{;组; } //最多50个字符
}

在我的表单绑定我的旅行对象像这样的列表:

 列表<旅游与GT;行进= logic.GetAllTravels();
DgvRecorridos.DataSource =旅行;

和我得到以下内容:

我想获得的服务,源城和命运,而不是城市。

的名称

先谢谢了。


解决方案

 列表<旅游与GT;行进= logic.GetAllTravels();
在旅行VAR _bind =从
            新选择
            {
                SERVICENAME = a.Service.Name,
                SOURCENAME = a.Source.Name,
                DestinyName = a.Destiny.Name,
                价格= a.Price
            };
DgvRecorridos.DataSource = _bind;

 列表<旅游与GT;行进= logic.GetAllTravels();
VAR _bind = travels.Select(A =>新建
            {
                SERVICENAME = a.Service.Name,
                SOURCENAME = a.Source.Name,
                DestinyName = a.Destiny.Name,
                价格= a.Price
            });
DgvRecorridos.DataSource = _bind;

i have a DataGridView and a list of objects that i would like to show.

The Objects are these:

public class Entity
{
    public int ID { get; set; }
}    

public class Travel: Entity
{
    public Service Service { get; set; }
    public City Source { get; set; }
    public City Destiny { get; set; }
    public decimal Price { get; set; }
}

public class Service: Entity
{
    public string Name { get; set; }
}

public class City: Entity
{
    public string Name { get; set; } // Max 50 chars
}

In my form i bind the list of of Travel Objects like this:

List<Travel> travels = logic.GetAllTravels();
DgvRecorridos.DataSource = travels;

And i get the following:

I would like to get the Name of the Service, Source City and Destiny City instead.

Thanks in advance.

解决方案

List<Travel> travels = logic.GetAllTravels();
var _bind = from a in travels
            select new
            {
                Servicename = a.Service.Name,
                SourceName = a.Source.Name,
                DestinyName = a.Destiny.Name,
                Price = a.Price
            };
DgvRecorridos.DataSource = _bind;

or

List<Travel> travels = logic.GetAllTravels();
var _bind = travels.Select(a => new 
            { 
                Servicename = a.Service.Name,
                SourceName = a.Source.Name,
                DestinyName = a.Destiny.Name,
                Price = a.Price
            });
DgvRecorridos.DataSource = _bind;

这篇关于绑定对象的DataGridView C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆