我正在使用here描述的技术将我的单选按钮组添加到我的C#winforms应用程序中。
该技术效果很好,直到我尝试标签大于9的单选按钮。
在这种情况下,单击单选按钮
时会发生错误System.InvalidOperationException未处理HResult = -2146233079
Message = DataBinding无法在列表中找到适合的行 所有绑定。 Source = System StackTrace: 在System.ComponentModel.ReflectPropertyDescriptor.SetValue(Object 组件,对象值) 在System.Windows.Forms.BindToObject.SetValue(对象值) 在System.Windows.Forms.Binding.PullData(布尔重新格式,布尔力) 在System.Windows.Forms.Binding.Target_PropertyChanged(Object sender,EventArgs e) 在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:第70行 在System.EventHandler.Invoke(Object sender,EventArgs e) 在System.Windows.Forms.RadioButton.OnClick(EventArgs e) 在System.Windows.Forms.RadioButton.OnMouseUp(MouseEventArgs mevent) 在System.Windows.Forms.Control.WmMouseUp(消息& m,MouseButtons按钮,Int32点击) 在System.Windows.Forms.Control.WndProc(消息& m) 在System.Windows.Forms.ButtonBase.WndProc(消息& 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所有者) 在e:\ EShared \ devnet10 \ VivSnap \ Main \ Form1.cs中的SBD.VivSnap.Main.Form1.btnForm1Go_Click(Object sender,EventArgs e):第36行 在System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 在System.Windows.Forms.Control.WmMouseUp(消息& m,MouseButtons按钮,Int32点击) 在System.Windows.Forms.Control.WndProc(消息& m) 在System.Windows.Forms.ButtonBase.WndProc(消息& m) 在System.Windows.Forms.Button.WndProc(消息& 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上下文) 在SB:.VivSnap.Main.Program.Main()在e:\ EShared \ devnet10 \ VivSnap \ Main \ Program.cs:第18行 在System.AppDomain._nExecuteAssembly(RuntimeAssembly程序集,String [] args) 在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback回调,对象状态,布尔值 preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean preserveSyncCtx) 在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态) 在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; }
My Radio Group课程如下 // 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
}
}
}
答案 0 :(得分:1)
问题是因为我有另一个依赖于所选值的属性绑定。 我有&#34; Machine.Mode&#34;作为绑定属性,其中Machine属性的类型取决于PrintType属性
答案 1 :(得分:1)
我在一个数据绑定组件中将.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");
}
}