数据绑定在列表中找不到一行适用于使用RadioButton组的所有绑定,其中组中有9个
-
21-12-2019 - |
题
我使用的是这里要将我的单选按钮组添加到我的c#winforms应用程序。
在这种情况下,在单击单选按钮时发生错误system.invalidOperationException未处理Hresult= -2146233079
Message= DataBinding无法在适合的列表中找到一行 所有绑定。 source= system stacttrace: 在system.ComponentModel.reflectPropertyDescriptor.setValue(对象 组件,对象值) 在system.windows.forms.bindtoObject.setValue(对象值) 在system.windows.forms.binding.pulldata(布尔重新格式化,布尔力) 在System.Windows.Forms.Binding.Target_PropertyChanged(对象发件人,Eventargs E) 在System.EventHandler.Invoke(对象发件人,Eventargs E) 在sbd.vivsnap.ui.radiogroupbox.radiobutton_checkedChanged(对象发件人, Eventargs E)在 E:\ eShared \ devnet10 \ vivsnap \ snapinui \ radidgroupbox.cs:第70行 在System.EventHandler.Invoke(对象发件人,Eventargs E) 在system.windows.forms.radiobutton.onclick(eventargs e) 在system.windows.forms.radiobutton.onmouseUp(MouseEventargs Mevent) 在System.Windows.Forms.Control.WmMeouseUp(Message&M,MouseButtons按钮,Int32 Clicks) 在system.windows.forms.control.wndproc(message&m) 在system.windows.forms.buttonbase.wndproc(message&m) 在system.windows.forms.nativewindow.debuggablecallback(Intptr hwnd,int32 msg,intptr wparam,intptr lparam) 在system.windows.forms.unsafenativemethods.dispatchmessagew(msg&msg) 在system.windows.forms.application.componentmanager.system.windows.forms.unsafenativemethods.imsocomponentmanager.fpushmessageloop(Intptr dwcomponentid,int32原因,Int32 pvloopdata) 在system.windows.forms.application.threadcontext.runmessageLoopinner(Int32 原因,ApplicationContext上下文) 在system.windows.forms.application.threadcontext.runmessageloop(int32 原因,ApplicationContext上下文) 在system.windows.forms.form.showdialog(IWIN32Window所有者) 在sbd.vivsnap.main.form1.btnform1go_click(对象发件人,eventargs e)中的e:\ eShared \ devnet10 \ vivsnap \ main \ form1.cs:第36行 在system.windows.forms.button.onmouseUp(MouseEventargs Mevent) 在System.Windows.Forms.Control.WmMeouseUp(Message&M,MouseButtons按钮,Int32 Clicks) 在system.windows.forms.control.wndproc(message&m) 在system.windows.forms.buttonbase.wndproc(message&m) 在system.windows.forms.button.wndproc(message&m) 在system.windows.forms.nativewindow.debuggablecallback(Intptr hwnd,int32 msg,intptr wparam,intptr lparam) 在system.windows.forms.unsafenativemethods.dispatchmessagew(msg&msg) 在system.windows.forms.application.componentmanager.system.windows.forms.unsafenativemethods.imsocomponentmanager.fpushmessageloop(Intptr dwcomponentid,int32原因,Int32 pvloopdata) 在system.windows.forms.application.threadcontext.runmessageLoopinner(int32 原因,ApplicationContext上下文) 在system.windows.forms.application.threadcontext.runmessageloop(int32 原因,ApplicationContext上下文) 在sbd.vivsnap.main.program.main()中的e:\ eShared \ devnet10 \ vivsnap \ main \ program.cs:line 18 在system.appdomain._nexecuteassembly(Runtimeassembly组装,String [] args) 在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAsseMbly() 在system.threading.executionContext.RunInternal(ExecutionContext ExecutionContext,ContextCallback回调,对象状态,布尔值 preservesyncctx) 在system.threading.executioncontext.run(executioncontext executioncontext,contexcallback回调,对象状态,布尔值 preservesyncctx) 在System.Threading.executionContext.Run(ExecutionContext ExecutionContext,ContexCallback回调,对象状态) 在system.threading.threadhelper.threadstart()InnerException:
绑定属性的代码是
groupBox.DataBindings.Add("Selected", dataSource, PrinterTypeNum, false, DataSourceUpdateMode.OnPropertyChanged);
.
我的属性代码是
public int PrinterTypeNum
{
get
{
try
{
return (int)this.PrinterType;
}
catch (Exception)
{
return 0;
}
}
set
{
try
{
this.PrinterType = (jtVivPrinterEnum)value;
// the enum goes from 0 to 15
}
catch (Exception)
{
this.PrinterType = jtVivPrinterEnum.jtVivPrinterUnknown;
throw;
}
}
}
public jtVivPrinterEnum PrinterType { get; set; }
.
**
[更新]我刚刚发现,如果属性如下,我就不会收到错误。
public int PrinterTypeNum { get; set; }
.
我的无线电小组类如下 // Databind在Winforms中的一组radiobuttons的最佳方式
public partial class RadioGroupBox : GroupBox
{
public RadioGroupBox()
{
this.InitializeComponent();
}
public event EventHandler SelectedChanged = delegate { };
int _selected;
public int Selected
{
get
{
return this._selected;
}
set
{
int val = 0;
var radioButton = this.Controls.OfType<RadioButton>()
.FirstOrDefault(radio => radio.Tag != null
&& int.TryParse(radio.Tag.ToString(), out val) && val == value);
if (radioButton != null)
{
try
{
radioButton.Checked = true;
this._selected = val;
}
catch (Exception ex)
{
Debug.Print(ex.ToString());
throw;
}
}
}
}
protected override void OnControlAdded(ControlEventArgs e)
{
base.OnControlAdded(e);
var radioButton = e.Control as RadioButton;
if (radioButton != null)
radioButton.CheckedChanged += this.radioButton_CheckedChanged;
}
void radioButton_CheckedChanged(object sender, EventArgs e)
{
var radio = (RadioButton)sender;
int val = 0;
if (radio.Checked && radio.Tag != null
&& int.TryParse(radio.Tag.ToString(), out val))
{
this._selected = val;
this.SelectedChanged(this, new EventArgs()); // raises error when val=10
}
}
}
. 解决方案
当我在数据绑定组件之一中将.Visible
属性更改为false时,我收到此错误。
在将.Visible
属性更改为false之前,绕过的工作是删除控件上的DataBindings,然后在让它们再次可见时添加数据绑定。
这是说明错误以及如何解决它的代码:
void SetupStuff()
{
BindingSource myBindingSource = new BindingSource();
// Retrieve my data into a DataTable and add it to the BindingSource
DataTable myDataTable = GetMyDataTable();
myBindingSource.DataSource = myDataTable.DefaultView;
// Add DataBindings to Visible components only
myTextBox.Visible = true;
myTextBox.DataBindings.Add("Text", myBindingSource, "MySecret");
}
void UpdateStuff()
{
// Update the currently selected data record
myDataTable.DefaultView[myBindingSource.Position].BeginEdit();
myDataTable.DefaultView[myBindingSource.Position]["MySecret"] = "Top Secret Meetup " + DateTime.Now.AddDays(1).ToString();
myDataTable.DefaultView[myBindingSource.Position].EndEdit();
myDataTable.DefaultView[myBindingSource.Position].AcceptChanges();
// if myTextBox.Visible, then we can see the message
}
void HideStuff()
{
myTextBox.Visible = false;
UpdateStuff();
// After myDataTable..AcceptChanges(), DataBindings are now BROKEN!
// This Cleared all data from controls bound to myBindingSource
try
{
myBindingSource.ResetCurrent();
}
catch (Exception brokenBindingSource)
{
Console.WriteLine("DataBinding broken" + brokenBindingSource.Message);
}
}
void HideStuff()
{
if (myTextBox.Visible)
{
myTextBox.DataBindings.Remove("MySecret");
myTextBox.Visible = false;
}
UpdateStuff();
// After myDataTable..AcceptChanges(), DataBindings are now BROKEN!
// This Cleared all data from controls bound to myBindingSource
try
{
myBindingSource.ResetCurrent();
}
catch (Exception brokenBiSo)
{
Console.WriteLine("DataBinding is broken" + brokenBiSo.Message);
}
}
void ShowStuff()
{
// If I want to make the component visible again, do this:
if (!myTextBox.Visible)
{
myTextBox.Visible = true;
myTextBox.DataBindings.Add("Text", myBindingSource, "MySecret");
}
}
. 其他提示
问题是因为我有另一个属性绑定,依赖于所选值。 我将“machine.mode”作为绑定属性,其中机器属性的类型依赖于PrintType属性