DataBinding cannot find a row in the list that is suitable for all bindings using RadioButton Group with over 9 in the group

StackOverflow https://stackoverflow.com//questions/23051112

  •  21-12-2019
  •  | 
  •  

Question

I am using the technique described here to add Groups of my radio buttons to my C# winforms application.

The technique works beautifully until I try a radio button with a tag greater than 9.

In this case an error occurs when i click the radio button

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:

My code for binding the property is

groupBox.DataBindings.Add("Selected", dataSource, PrinterTypeNum, false, DataSourceUpdateMode.OnPropertyChanged);

My code for the property is

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; }

**

[Update] I just discovered that I do not get the error if the property is just as follows.

public int PrinterTypeNum { get; set; }

My Radio Group class is as follows //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
        }
    }
}
Was it helpful?

Solution

I got this error when I changed the .Visible property to false in one of data bound components.

The work around is to Remove DataBindings on controls before changing their .Visible property to false, then adding the DataBindings back when making them visible again.

Here is code to illustrate the error and how to work around it:

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");
    }
}

OTHER TIPS

The problem was because I had another property bound that depended on the value selected. I had "Machine.Mode" as a bound property where the type of the Machine property depended on the PrintType property

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top