2010-06-26 130 views
0

Im使用List<Patient>對象作爲數據源到數據網格視圖。我需要在Patient類中使用兩個字符串屬性的串聯作爲單個文本框列的值。 我知道這可以通過在webforms GridView中使用OnRowDataBound事件來完成。如何處理勝利形式的這種情況?我不能看到Win窗體數據網格視圖中的OnRowDataBound事件。數據綁定到DataGridView

爲了澄清我的病人類,

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

我需要這兩個屬性的組合綁定到網格視圖稱爲「名稱」一列。還有Patient類中的一些其他屬性,它們使用列的DataPropertyName直接映射到數據網格視圖列。因此,以編程方式填充所有列是很繁瑣的。

在此先感謝您的幫助。

回答

1

一個簡單的解決方案是添加一個新屬性,它將計算值(視圖)模型並綁定到此屬性。

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

    public string InitialsAndLastName 
    { 
     get { return this.Initials + " " + this.LastName; } 
    } 
} 
+0

是的。這比進行事件要容易得多。 – 2010-06-26 12:59:45