I'm using Entity Framework for the first time (database first, entity framework 5) in a windows forms application (vs2010, .net 4). I am having trouble with the binding between my entity object and the windows forms controls. I have textbox, datetimepicker and combobox controls. When I open a window with the bound controls, the correct data is displayed in the controls. However, when I change the value in one of my controls and tab off the control, the value reverts to the original value in the control as if the value is not getting pushed to the object. Here are code exerpts:
My entity object:
namespace Entities
{
using System;
using System.Collections.Generic;
public partial class ExternalDocument
{
public int ExternalDocumentID { get; set; }
public bool Active { get; set; }
public bool Closed { get; set; }
public Nullable<int> CompanyID { get; set; }
public Nullable<int> ContactID { get; set; }
public string DocumentNbr { get; set; }
public Nullable<System.DateTime> DocumentDate { get; set; }
public Nullable<System.DateTime> DateReceived { get; set; }
public virtual Company Company { get; set; }
public virtual Contact Contact { get; set; }
}
}
The data binding:
private void SetDataBindings()
{
LoadComboBoxValues();
this.textDocumentNbr.DataBindings.Add("Text", this.document, "DocumentNbr");
this.textDocumentNbr.Leave += new EventHandler(textDocumentNbr_Leave);
this.dateDocument.DataBindings.Add(new Binding("Value", this.document, "DocumentDate"));
this.dateReceived.DataBindings.Add("Value", this.document, "DateReceived");
this.comboCompanyID.DataBindings.Add("SelectedValue", document, "CompanyID");
}
I have wondered if there is an entity framework error when the object property is set but I have not been able to figure out a good way to trap any such errors. My entity framework object does not have the On< PropertyName >Changing methods that are created for earlier versions of entity framework. I have been trying to trap errors when focus leaves the control but think this can't be the best method:
private void dateDocument_Leave(object sender, EventArgs e)
{
string errorString = this.entitiesController.GetValidationErrors();
this.errorDocumentDate.SetError(this.dateDocument, errorString);
}
public string GetValidationErrors()
{
string errorString = "";
List<DbEntityValidationResult> errorList = (List<DbEntityValidationResult>)this.finesse2Context.GetValidationErrors();
if (errorList.Count > 0)
{
foreach(var eve in errorList)
{
errorString += "Entity of type " + eve.Entry.Entity.GetType().Name + " in state" + eve.Entry.State + " has the following validation errors:"; ;
foreach (var ve in eve.ValidationErrors)
{
errorString += "- Property: " + ve.PropertyName + " Error: " + ve.ErrorMessage;
}
}
}
return errorString;
}
Any help would be appreciated. Thanks!