2014-06-25 47 views
0

基於在EF6數據庫值我創建使用EF6。我的模型的模型文字是這樣的:如何使用返回get方法

public partial class Good 
{ 
    public Good() 
    { 
     this.InnoviceDetails = new HashSet<InvoiceDetail>(); 
    } 

    public int Id { get; set; } 
    public string Serial { get; set; } 
    public string HasTax { get; set; } 
    public string InitialAmount { get; set; } 
    public string Group { get; set; } 

    public virtual ICollection<InvoiceDetail> InnoviceDetails { get; set; } 
} 

其中一個我列的是HasTax和值這是和,但在mygridview我需要更改這些值(0,1)。我的意思是如果數值是1顯示如果是0顯示。我想要使用get我的模型的方法做這些嗎?可能嗎?

Get方法檢查數據庫中的值並將適當的值返回到gridview或表示層?

問候

+0

你用什麼UI? Windows窗體,WPF,還是它是一個ASP.NET頁面? –

+0

我使用Windows窗體 –

+0

類似的問題已經被問到 - 嘗試閱讀:http://stackoverflow.com/questions/15267666/entity-framework-code-first-convert-between-class-boolean-and-column-integer ; http://stackoverflow.com/questions/19370104/convert-value-when-mapping; http://stackoverflow.com/questions/6708996/convert-from-to-string-in-database-to-boolean-property-entity-framework-4-1/6709186#6709186 –

回答

0

如果您:

1)不要想直接更改模型或創建視圖。
2)或添加將只用於綁定轉換的任何附加屬性。

然後你留下了我能想到的兩個主要變量:

1)創建一個半包裝,暴露你的原始模型和附加特殊屬性的綁定與轉換:

public class ModelSemiWrapperBase<TModel> 
{ 
    public ModelSemiWrapperBase(TModel model) 
    { 
     this.Model = model; 
    } 
    public TModel Model 
    { 
     get; 
     private set; 
    } 
} 

public class GoodSemiWrapper : ModelSemiWrapperBase<Good> 
{ 
    public String HasTax 
    { 
     get 
     { 
      return (this.Model.HasTax == 0) ? ("Yes") : ("No"); 
     } 
     set {...} 
    } 
} 

只是不要忘記INotifyPropertyChanged並更改通知。同樣在這種情況下,您將不得不在DataGridView中手動添加列。

2)處理事件在DataGridView:

喜歡的東西How do I use a ValueConverter with Databinding in Winforms,只是使用CellValueChangedCellValidatingDataBindingComplete或其他類似events的dataGridView。
我甚至不確定你應該真正使用哪些事件。我不會用這種方式。它太容易出錯,它強烈地將dataGridView與驗證和轉換邏輯捆綁在一起。