我正在使用描述的技术 here将单选按钮组添加到我的 C# winforms 应用程序。
该技术工作得很好,直到我尝试使用标签大于 9 的单选按钮。
在这种情况下,当我单击单选按钮时会发生错误
System.InvalidOperationException was unhandled HResult=-2146233079
Message=DataBinding cannot find a row in the list that is suitable for all bindings. Source=System StackTrace: at System.ComponentModel.ReflectPropertyDescriptor.SetValue(Object component, Object value) at System.Windows.Forms.BindToObject.SetValue(Object value) at System.Windows.Forms.Binding.PullData(Boolean reformat, Boolean force) at System.Windows.Forms.Binding.Target_PropertyChanged(Object sender, EventArgs e) at System.EventHandler.Invoke(Object sender, EventArgs e) at SBD.VivSnap.UI.RadioGroupBox.radioButton_CheckedChanged(Object sender, EventArgs e) in e:\EShared\devnet10\VivSnap\SnapInUI\RadioGroupBox.cs:line 70 at System.EventHandler.Invoke(Object sender, EventArgs e) at System.Windows.Forms.RadioButton.OnClick(EventArgs e) at System.Windows.Forms.RadioButton.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Form.ShowDialog(IWin32Window owner) at SBD.VivSnap.Main.Form1.btnForm1Go_Click(Object sender, EventArgs e) in e:\EShared\devnet10\VivSnap\Main\Form1.cs:line 36 at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at SBD.VivSnap.Main.Program.Main() in e:\EShared\devnet10\VivSnap\Main\Program.cs:line 18 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException:
我的绑定(bind)属性的代码是
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; }
我的 radio 组类(class)如下 //Best way to databind a group of radiobuttons in WinForms
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
}
}
}
最佳答案
当我在数据绑定(bind)组件之一中将 .Visible
属性更改为 false 时,出现此错误。
解决方法是在将控件的 .Visible
属性更改为 false 之前删除控件上的 DataBindings,然后在使其再次可见时添加 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");
}
}
关于winforms - DataBinding 在列表中找不到适合使用组中超过 9 个单选按钮组的所有绑定(bind)的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23051112/