Data Binding to Hidden Controls

rjp2008

New member
Joined
Feb 28, 2008
Messages
4
Programming Experience
Beginner
Hi, I have a windows form that I bind to a datatable via a binding source. The binding is done in code at run time. Eg;

Me.txtCode.DataBindings.Add(New System.Windows.Forms.Binding("Text", bsForm, "ToolCode", True))

(bsForm is the binding source)

This works well except if the form control is hidden (I have a couple controls on the form that hold Record ID and Version numbers that the user doesn't need to see). Databinding doesn't seem to work reliably for hidden controls.

Has anybody got a solution to this problem?

Thanks in Advance

Richard
 
There is probably a better way to use the data your needing. How about telling how you use the data on the form. If we can't come up with reasonable answer I have a cheap workaround, but would rather see if there is a function or query that would fix this for you.

Welcome, and if you have not already, read the posting guides. :cool:
 
Last edited:
newguy thanks for your reply.

I have a form that displays one record at a time and is basically bound to a datatable within a dataset. My code manually populates the datatable. Any updates, I manually write to the backend DB. I haven't used any of the drag and drop data binding facilities, my code handles all the SQL stuff.

Each database record has a unique ID number and also a RowVersion number (same as a timestamp in SQL Server) which is incremented each time the record is updated.

I am using two hidden bound controls on my form to hold the ID and RowVersion values. When the user hits the Save button, my code uses the ID value in the update query to identify the correct record. The rowversion is compared to the value held by the backend database, if they are different I know that another user has already updated the record since the data was loaded into the dataset and I have to deal with the potential conflict.

So in summary, I want to use the hidden text controls to keep track of these two values (ID and RV). Works fine if the fields are visible, but not if they are hidden.

Any ideas?
 
I think I recognize your setup. You could set 2 variables(class level) that change with the row changes - instead of the textbox holding the value, then in your update statement use the variable names instead of the textbox.text. They will be holding the reference to the ID for the update. "UPDATE <yourTable> SET x = @x, y = @y WHERE ID = @ID" <- this being the variable. If your using parameters add it there. Or however you build your SQL statements.
 
...You could set 2 variables(class level) that change with the row changes...

You hit the nail on the head there, that's the key issue, keeping track of which record the user is working upon. Its easy for simple record navigation, but my form enables the user to apply filters, sorts, searches and do deletions.

I hold the variables in text boxes because that way binding takes care of everything as the user navigates through the Datatable - providing that is the binding works, but it doesn't for hidden text boxes :(

Is there an easy way to keep the values in the two variables you mention in sync (bound) with the current DataTable record?
 
Should be, you made the nav button your self right? You will need a variable(integer) to keep tract of which record your on. If you have a moveLast you will set it to <yourDataset>.Rows.Count -1. MoveFirst - set it to 0. Add or subtract with your next and previous buttons. So to keep up your ID is:
VB.NET:
ID = yourDataset.Tables(<tableName>).Rows(<yourIndexVariable).Item(<yourcolumnname>)
 
rjp2008

In the designer set the visiblity to true. Then in code just after you do the databindings for the the hidden textbox, set the visibility to false.

I did a quick little mock up and this worked for me.
 
Don't use hidden controls at all. You're using a BindingSource. You can always get access to the current record via its Current property. It will return a DataRowView and you can then get your ID and version values from it directly. Hidden controls are BAD! Controls are UI elements. That's what they should be used for.
 
That is what I was trying to avoid - the use of hidden controls. Mine works, but I like your solution JMC, not sure why I didn't think of that.
 
Thanks for all the replies guys.

jmc, i'd started to think along those lines myself, thanks for enlightening me that it's the bindingsource current property I need to look at. And I agree, use of hidden form variables isn't the most elegant solution, although i've recently started to get into ASP.net and I see its a technique used there to hold state info.

demausdauth - I saw that mentioned somewhere so I tried it in my code, setting the visibility to false after databinding. And indeed it works most of the time, but just occaisionally during testing I found the binding was not taking place, and I couldn't work out why.

My plan is to implement jmc's solution but as always i'm under time pressure and I need a q&d fix that will work for now. What I plan to do is leave the variables visible but use the old trick of setting the background and foreground colours to be the same as form background, so the user can't see them. I know it ain't pretty but it'll minimise the changes to my code and I can put something better in place when I start on the next phase of the project.
 
ASP.NET is a different kettle of fish. There is a dedicated HTML tag for storing data that you don't want the user to see but you'll need to access later. You don't use a TextBox and set its visibility to false. In WinForms you just use a variable to store such data.
 
Back
Top