我有一个绑定到Race记录的LookUpEdit控件。它有Car记录的查找数据源,我正在尝试将Race.CarId设置为我的表单初始化中Cars数据源中的第一条记录,但我无法以任何方式实现它。
// leCallsign is a lookup editor.
// setting datasource - an array of cars
leCallsign.Properties.DataSource = bsCar;
leCallsign.Properties.ForceInitialize();
leCallsign.Properties.ValueMember = "Id";
leCallsign.Properties.DisplayMember = "Callsign";
// bind Race's CarId to datasource's Id
leCallsign.DataBindings.Add(new Binding("EditValue", bsRace, "CarId", false, DataSourceUpdateMode.OnPropertyChanged));
// set editor value as the first of cars Id.
// Because of binding above it hopefully shold update the underlying bsRace record...
leCallsign.EditValue = ((Car)bsCar[0]).Id;
// but it does not neither here
leCallsign.DoValidate();
leCallsign.ResetBindings();
bsRace.ResetBindings(false);
// nor after any of the lines above
我尝试使用DataSourceUpdateMode.OnPropertyChanged和DataSourceUpdateMode.OnValidation,但它仍无法正常工作。
答案 0 :(得分:0)
当我将代码从表单构造函数移动到Load()事件处理程序时,问题就消失了。