BindingSource to control - code only

NachitoMax 416 Reputation points
2021-10-01T14:05:18.717+00:00

Hi

I currently have a BindingSource object on my form that is populated by a class of properties. Each control has its DataBinding EditValue set as the BindingSource.Field.

However the BindingSource Control is giving design time render issues and my controls become invisible and the only way to see them is to close the form class and re-open it. When i delete the BindingSource control i dont have this render issue.

I thought it would be better to create the BindingSource at runtime and set each controls DataBinding EditValue to the BindingSource but i cant get it right. If you look at this image, you can see the EditValue is set to the xxxBindingSource._Finish-color2 field. In code, EditValue is not available as an assignable property
136997-editvalue.png

i can create the BindingSource when my form opens

Private bs As New BindingSource  

I can assign a BindingList as the DataSource (the same binding list i am assigning to the BindingSource Control Datasource)

bs.DataSource = JBindingList  

I tried this appraoch but nothing happens-

TxtType.DataBindings.Add(New Binding("Text", bs, "_Finish_color2"))  

but nothing happens so, what is the right way to assign a BindingSource field to a control in code?

Thanks

Developer technologies | .NET | Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 61,116 Reputation points
    2021-10-01T15:34:40.14+00:00

    Generally the easiest thing to do is bind it via the designer. Then look at the generated code.

    In your case the syntax looks correct but I'm concerned about the property you're binding to. You really have a property called _Finish_color2 that is public facing? This looks like a field name to me. But if the name is wrong I would expect the binding to fail. But if you're doing that binding in the OnLoad then the exception is silently eaten and you wouldn't know.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.