问题描述
在VS2012中的表单上,我有一个绑定源控件(称为binding),并绑定了DateTimePicker控件。
对于绑定属性,我的MinDate = 1 / 01/1753和MaxDate = 31/12/9998
已通过从日历中选择今天设置了价值5/04/2013 11:27 AM
I使用
var dset = base.Context.ContactEvents设置绑定源
var qry = dset.Where(p => p.Id> 0).OrderBy(x => x.Id);
qry.Load();
this.bindingSource.DataSource = dset.Local.ToBindingList();
绑定源的使用方式如下:
public void RefreshBindingDataSourceAndPosition(BindingSource binding)
{
binding.DataSource = this.bindingSource.DataSource; //这里出现错误
binding.Position = this.bindingSource.Position;
}
错误信息为
我可以通过不绑定数据选择器并将其设置在EventsBindingSource_CurrentChanged事件中来解决此问题, / p>
但是必须这样做似乎很奇怪。如何使数据绑定正常工作?
[更新]
此问题类似于
我试图在一个较简单的项目中重现该问题,以便尝试找出原因,但是在较简单的项目中它起作用。项目也可以在另一台计算机上工作。
在同时使用SQL Server 2012和2008R2的计算机上会出现问题。我尝试在控制面板中更改日期格式和国家/地区。我也尝试了format属性的不同设置。我还尝试将日期字段设置为支持null。
当我将错误复制到剪贴板时,它显示以下内容;
我的EF类别如下
公共类ContactEvent:LoggedEntity
{
public virtual SortableBindingList< ContactEventAttendee>与会者{私人套装; }
public virtual ContactEventType ContactEventType {get;组; }
public string Details {get;组; }
公共DateTime? EventTime {get;组; }
公共虚拟SortableBindingList< ContactEventItem>项目{get;私人套装; }
public int State {get;组; }
公用字串Title {get;组; }
公共替代字符串ToString()
{
返回联系事件;
}
}
它是从
继承的
公共抽象类LoggedEntity
{
public LoggedEntity()
{
this.RowId = Guid.NewGuid();
this.RowVersionId = 0;
AppDomain dm = AppDomain.CurrentDomain; //获取当前线程的当前应用程序域。
对象s = AppDomain.CurrentDomain.GetData( SiteNumber);
this.SourceSiteNumber = Convert.ToInt32(s);
}
public LoggedEntity(int SiteNumber)
{
//下面的3个用于标识版本
this.RowId = Guid.NewGuid ();
this.RowVersionId = 0;
this.SourceSiteNumber = SiteNumber;
}
public ind Id {get;组; }
公共Guid RowId {get;组; }
[ConcurrencyCheck]
public int RowVersionId {get;组; }
public int SourceSiteNumber {get;组; }
}
[更新]
类似的问题是
[更新]
另一个
,打开调试选项后,我发现一个错误
无效的对象名称 dbo .__ MigrationHistory。
但是这是EF5中的已知错误
[更新]:我发现了另一个有类似未解决问题的人
发现我在运行.EXE时没有问题
[更新]我可以通过禁用跨应用程序域或托管/本机异常时中断来跳过错误边界
在工具->选项->调试->常规中
[更新]我添加了以下内容,以便可以检查控件属性。
引起异常的原因可能是DatePicker的DataBinding值属性已设置为BindingSource字段。
仅需要设置DatePicker的DataBinding文本属性,数据绑定才能正常工作。
检查DatePicker的DataBinding值属性字段中是否有值,一旦删除该问题,该问题就会消失。
I have a bindingsource control called binding, on a form in VS2012, and a DateTimePicker control bound to it.
for the binding properties I have MinDate = 1/01/1753 and MaxDate = 31/12/9998Value has been set by picking Today from the calender 5/04/2013 11:27 AM
I set a bindingsource up using
var dset = base.Context.ContactEvents;
var qry = dset.Where(p => p.Id > 0).OrderBy(x => x.Id);
qry.Load();
this.bindingSource.DataSource = dset.Local.ToBindingList();
The bindingsource is used in the following manner;
public void RefreshBindingDataSourceAndPosition(BindingSource binding)
{
binding.DataSource = this.bindingSource.DataSource; // error raised here
binding.Position = this.bindingSource.Position;
}
The error information is
I can work around the problem by not binding the Data Picker , and setting it in the EventsBindingSource_CurrentChanged event
However it seems odd to have to do this. How can I get the databinding working?
[Update]This problem is similar to the one described hereI have tried to reproduce the problem in a simpler project so as to try and isolate the cause, however it works in the simpler project. Also the project works on another computer.The problem occurs on my computer with both SQL Server 2012 and 2008R2. I have tried altering the date format and country in control panel. Also I have tried different settings for the format property. I have also tried setting the date field to support null.
When I copy the error to the clipboard it shows the following ;
My EF class is as follows
public class ContactEvent : LoggedEntity
{
public virtual SortableBindingList<ContactEventAttendee> Attendees { get; private set; }
public virtual ContactEventType ContactEventType { get; set; }
public string Details { get; set; }
public DateTime? EventTime { get; set; }
public virtual SortableBindingList<ContactEventItem> Items { get; private set; }
public int State { get; set; }
public string Title { get; set; }
public override string ToString()
{
return "Contact Events";
}
}
it inherits from
public abstract class LoggedEntity
{
public LoggedEntity()
{
this.RowId = Guid.NewGuid();
this.RowVersionId = 0;
AppDomain dm = AppDomain.CurrentDomain; // Gets the current application domain for the current Thread.
object s = AppDomain.CurrentDomain.GetData("SiteNumber");
this.SourceSiteNumber = Convert.ToInt32(s);
}
public LoggedEntity(int SiteNumber)
{
// the following 3 are used to identify the version
this.RowId = Guid.NewGuid();
this.RowVersionId = 0;
this.SourceSiteNumber = SiteNumber;
}
public int Id { get; set; }
public Guid RowId { get; set; }
[ConcurrencyCheck]
public int RowVersionId { get; set; }
public int SourceSiteNumber { get; set; }
}
[update]A similar problem is here
[update]Another here makes me think I need to look at how keys are being processed.
[update]I noticed the following in the output window
A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in System.Windows.Forms.dll
[update]This led me to here
and after turning on the debug options I found an error
Invalid object name 'dbo.__MigrationHistory'.
however that is a known bug in EF5
[Update]: I found another person with similar unsolved issues hereDiscovered I dont have problems when running the .EXE
[update] I can skip over the error by disabling "Break when exceptions cross App Domain or managed/native boundaryin Tools->Options->Debugging->General
[update] I adding the following, so I could inspect the control properties.
The cause of the exception may be that the DatePicker's DataBinding "Value" property has been set to a BindingSource field.Only the DatePicker's DataBinding "Text" property needs to be set for the data binding to work correctly.Check if there is a value in the DatePicker's DataBinding "Value" property field, once removed the issue should go away.
这篇关于DatePicker.Value.Set错误绑定到数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!