文章类型: .NET
关键词: c#,WinForm,DataGridView,嵌套绑定,解决办法
内容摘要: C#中的WinForm开发中,对于控件DataGridView如何实现嵌套对象绑定到列,在Web开发中一般都是:DataGridView对象.DataPropertyName="字段名或对象属性名";如果集合中包含对象属性的话,使用:DataGridView对象.DataPropertyName="子对象.属性名";其中子对象是绑定DataGridView数据源的属性对象。但是在C#的WinForm开发中,是不支持嵌套绑定的,DataGridView只能绑定一层。这样就不能使用DataGridView对象.DataPropertyName="子对象.属性名"来进行数据的绑定了。如何进行解决呢?下面给出解决办法。

c#的WinForm开发中控件DataGridView的嵌套绑定解决办法

2016/1/27 1:55:20    来源:apple    阅读:

C#中的WinForm开发中,对于控件DataGridView如何实现嵌套对象绑定到列,在Web开发中一般都是:DataGridView对象.DataPropertyName="字段名或对象属性名";如果集合中包含对象属性的话,使用DataGridView对象.DataPropertyName="子对象.属性名";其中子对象是绑定DataGridView数据源的属性对象。但是在C#的WinForm开发中,是不支持嵌套绑定的,DataGridView只能绑定一层。这样就不能使用DataGridView对象.DataPropertyName="子对象.属性名"来进行数据的绑定了。如何进行解决呢?下面给出解决办法!

可以用Linq进行转换下,把嵌套的属性提升到顶层,比如:
var listChange = list.Select(x => new { x.普通属性1, x.普通属性2, 子对象_属性=x.子对象.属性 ... });
然后可以绑定:
dataGridView.DataSource = listChange.ToList();
dataGridView.DataPropertyName="子对象_属性";

具体可以参考下面的代码:

前台的代码:

private System.Windows.Forms.DataGridView dgvCheckItemManager;
// 
// Col_ID
// 
this.Col_ID.DataPropertyName = "ID";
this.Col_ID.HeaderText = "检查项编号";
this.Col_ID.Name = "Col_ID";
this.Col_ID.ReadOnly = true;
this.Col_ID.Visible = false;
// 
// Col_CheckItemName
// 
this.Col_CheckItemName.DataPropertyName = "CheckItemName";
this.Col_CheckItemName.HeaderText = "检查项名称";
this.Col_CheckItemName.Name = "Col_CheckItemName";
this.Col_CheckItemName.ReadOnly = true;
this.Col_CheckItemName.Width = 180;
// 
// ColCheckItemPinyin
// 
this.ColCheckItemPinyin.DataPropertyName = "Acronym";
this.ColCheckItemPinyin.HeaderText = "拼音简写";
this.ColCheckItemPinyin.Name = "ColCheckItemPinyin";
this.ColCheckItemPinyin.ReadOnly = true;
this.ColCheckItemPinyin.Width = 180;
// 
// Col_UnitPrice
// 
this.Col_UnitPrice.DataPropertyName = "UnitPrice";
this.Col_UnitPrice.HeaderText = "单价";
this.Col_UnitPrice.Name = "Col_UnitPrice";
this.Col_UnitPrice.ReadOnly = true;
// 
// Col_DoctorProjectName
// 
this.Col_DoctorProjectName.DataPropertyName = "DoctorProjectName";
this.Col_DoctorProjectName.HeaderText = "项目名称";
this.Col_DoctorProjectName.Name = "Col_DoctorProjectName";
this.Col_DoctorProjectName.ReadOnly = true;
// 
// ColCheckItemCategory
// 
this.ColCheckItemCategory.DataPropertyName = "Description";
this.ColCheckItemCategory.HeaderText = "描述";
this.ColCheckItemCategory.Name = "ColCheckItemCategory";
this.ColCheckItemCategory.ReadOnly = true;
this.ColCheckItemCategory.Width = 180;


后台的.cs代码如下:

//CheckItem实体类如下
public class CheckItem
{
    public CheckItem()
    {
        if (doctorProject == null)
        {
            doctorProject = new DoctorProject();
        }
    }

    private int iD = -1;

    public int ID
    {
        get { return iD; }
        set { iD = value; }
    }

    private string checkItemName = string.Empty;

    public string CheckItemName
    {
        get { return checkItemName; }
        set { checkItemName = value; }
    }

    /// <summary>
    /// 药品名称拼音首字母
    /// </summary>
    private string acronym = string.Empty;

    public string Acronym
    {
        get { return acronym; }
        set { acronym = value; }
    }

    private decimal unitPrice = 0;

    public decimal UnitPrice
    {
        get { return unitPrice; }
        set { unitPrice = value; }
    }

    private string description = string.Empty;

    public string Description
    {
        get { return description; }
        set { description = value; }
    }

    private DoctorProject doctorProject;

    public DoctorProject DoctorProject
    {
        get { return doctorProject; }
        set { doctorProject = value; }
    }
}

绑定DataGridView的代码如下:

List<CheckItem> checkItemList = CheckItemManager.GetAllCheckItem(ref retMsg);
//注意:DataGridView只能绑定一层。可以用linq转换下,把嵌套的属性提升到顶层
var checkItemListChange = checkItemList.Select(x => new {x.ID, x.CheckItemName, x.Acronym, x.UnitPrice, x.DoctorProject.DoctorProjectName, x.Description });   
dgvCheckItemManager.DataSource = checkItemListChange.ToList();

如有问题,请留言,可以协助您解决问题。

↑ 上一篇文章:C#的WinForm中回车键事件写法 关键词:C#,Winform,回车键,enter,事件,写法,K.. 发布日期:2016/1/26 21:05:57
↓ 下一篇文章:进行远程桌面连接无法最大化的解决办法 关键词:远程桌面,无法,最大化,解决,办法 发布日期:2016/1/29 22:15:27
相关文章:
过滤的DataGridView在不改变数据源的分析 关键词:c#,winforms,visual,studio,datagridview,filter,过滤.. 发布日期:2016-02-04 21:34
Winform中ListView鼠标移动使用ToolTip动态显示信息 关键词:C#,Winform,ListView,鼠标移动,ToolTip,动态,显示,位置,信息 发布日期:2015-07-21 16:05
C#的WinForm开发中完美解决窗体中的各个控件同比自动放缩大小 关键词:C#,Winform,窗体,各个控件,同比例,自动放缩大小,完美解决 发布日期:2016-01-22 19:11
相关目录:.NET软件开发
我要评论
正在加载评论信息......