【问题标题】:Data Binding to DataGridView数据绑定到 DataGridView
【发布时间】:2010-06-26 11:52:54
【问题描述】:

我使用List<Patient> 对象作为数据网格视图的数据源。我需要使用Patient 类中两个字符串属性的串联作为单个文本框列的值。 我知道这可以通过在 webforms GridView 中使用OnRowDataBound 事件来完成。如何以获胜形式处理这种情况?我在 win 表单数据网格视图中看不到 OnRowDataBound 事件。

为了澄清,我的 Patient 类是,

public class Patient
{
    public string Initials { get; set; }
    public string LastName { get; set; }
}

我需要将这两个属性的组合绑定到网格视图中名为“名称”的单个列。 Patient 类中还有一些其他属性,它们使用列的 DataPropertyName 直接映射到数据网格视图列。所以以编程方式填充所有列是很乏味的。

【问题讨论】:

    标签: c# data-binding datagridview


    【解决方案1】:

    一个简单的解决方案是添加一个新属性来计算您的(视图)模型的值并绑定到该属性。

    public class Patient
    {
        public string Initials { get; set; }
        public string LastName { get; set; }
    
        public string InitialsAndLastName
        {
            get { return this.Initials + " " + this.LastName; }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-12
      • 1970-01-01
      • 2014-12-30
      • 2020-03-11
      相关资源
      最近更新 更多