0

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!

4

1 回答 1

0

事实证明,除非在绑定上指定了“formattingEnabled”,否则当绑定将不可为空的值推送到对象中的可空属性时会收到异常。

因此,像这样的绑定有效:

this.dateDocument.DataBindings.Add(new Binding("Value", this.document, "DocumentDate", true));

而这不是:

this.dateDocument.DataBindings.Add(new Binding("Value", this.document, "DocumentDate"));

我仍然不清楚如何捕获这种类型的错误,因为绑定只是捕获错误并将控件中的值替换为原始值。

于 2013-03-06T21:07:19.760 回答