C# 如何使用get方法根据EF6中数据库中的值返回文本
我使用EF6创建了一个模型。我的模型如下:C# 如何使用get方法根据EF6中数据库中的值返回文本,c#,entity-framework,gridview,model,presentation-layer,C#,Entity Framework,Gridview,Model,Presentation Layer,我使用EF6创建了一个模型。我的模型如下: 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;
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; }
}
公共部分类良好
{
公益
{
this.innovicedeails=new HashSet();
}
公共int Id{get;set;}
公共字符串序列{get;set;}
公共字符串HasTax{get;set;}
公共字符串InitialAmount{get;set;}
公共字符串组{get;set;}
公共虚拟ICollection InnoviceDetails{get;set;}
}
我的其中一列是HasTax,它的值是1和0,但在mygridview中,我需要更改这些值(0,1)。我的意思是,如果值为1
则显示Yes,如果值为0
则显示No。我想使用我的模型的get
方法执行这些操作?可能吗
Get方法检查数据库中的值,并将正确的值返回到gridview
或表示层
如果您:
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 {...}
}
}
公共类ModelSemiWrapperBase
{
公共模型SemiWrapperBase(TModel模型)
{
这个模型=模型;
}
公共TModel模型
{
收到
私人设置;
}
}
公共类GoodSemiWrapper:ModelSemiWrapperBase
{
公共税收
{
收到
{
返回(this.Model.HasTax==0)?(“是”):(“否”);
}
集合{…}
}
}
只是不要忘记INotifyPropertyChanged和changes通知。在这种情况下,还必须在DataGridView中手动添加列
2) 在DataGridView中处理事件:
类似于,仅使用dataGridView,或其他类似功能。我甚至不确定你应该真正使用哪些事件。我没有用这种方式去。它太容易出错,并且将dataGridView与验证和转换逻辑紧密捆绑在一起。您使用什么UI?Windows窗体、WPF或是ASP.NET页面?已经有人问过类似的问题-请尝试阅读:;“使用我的模型的get方法”是什么意思?我是说我检查了get方法中的值,因为get方法返回的是Object的值,您是指属性(Int32 value{get;})中的getter方法,还是加载实体的某个加载方法?