0

I create a model using EF6 .My model is like this :

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; }
}

One of the my columns is HasTax and the value of this is 1 and 0 ,but in mygridview i need to change these values (0,1). i mean if the value is 1 shows Yes and if it is 0 shows No.I want to do these using get method of my model ?Is it possible ?

Get method checks the values in database and return proper values to gridview or Presentation layer ?

Best regards

Ehsan Akbar
  • 6,977
  • 19
  • 96
  • 180
  • What UI do you use? Windows Forms, WPF, or is it an ASP.NET page? – Eugene Podskal Jun 25 '14 at 11:17
  • Similar questions have been already asked - try to read: 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 – Eugene Podskal Jun 25 '14 at 11:44
  • And what do you mean by 'using get method of my model'? – Eugene Podskal Jun 25 '14 at 11:46
  • I mean i checked the value in get method ,because the get method returns value of an object – Ehsan Akbar Jun 25 '14 at 11:48
  • You mean getter method in property(Int32 Value{get;}) or some loading method that loads your entities? – Eugene Podskal Jun 25 '14 at 11:52
  • some loading method that loads my entities – Ehsan Akbar Jun 25 '14 at 11:53
  • I have given you those links - have you tried them? There are not many ways to do what you want in UI-agnostic way. And I doubt that someone can produce anything different from what is already proposed. – Eugene Podskal Jun 25 '14 at 11:57
  • yes but it just for avoiding database mapping – Ehsan Akbar Jun 25 '14 at 11:58

1 Answers1

0

If you:

1) Don't want to directly change your model or create views.
2) Or add any additional properties that will be used only for binding-with-conversion.

Then you are left with two main variants I could think of:

1) Create a semi-wrapper that exposes your original model and additional special property for binding-with-conversion:

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 {...}
    }
}

Just do not forget about INotifyPropertyChanged and changes notification. Also in this case you will have to manually add columns in the DataGridView.

2) Handle events in the DataGridView:

Something like How do I use a ValueConverter with Databinding in Winforms, just with dataGridView using the CellValueChanged, CellValidating, DataBindingComplete or other similar events.
I am not even sure which events you should really use. And I'd not gone using such a way. It is too error-prone and it strongly bundles your dataGridView with validation and conversion logic.

Community
  • 1
  • 1
Eugene Podskal
  • 10,270
  • 5
  • 31
  • 53