Finding the control bound to a specific field in a DataTable
Posted on 2007-03-19
I'm writing a VB.Net application and have the following problem. I have a form with several textboxes on it. I also have a DataTable on the form (Private dt as Data.DataTable). The textboxes are bound to the datatable. (Only the Text Properties).
I do the binding like this: txtLastName.DataBindings.Add("Text", DataTableName, "LastName")
I need to add some information relating to the field being displayed by each textbox inside the tag of the textbox. So in the example above, txtLastName.Tag will have some data that describes the LastName field.
My problem is: when the time comes to save and I detect that the LastName field has changed, I need to get the information stored inside the tag of txtLastName. How can I find out what textbox is bound to the LastName field in the datatable? I'll need a reference to that textbox so that I can get the tag.
I was hoping that the form's binding context, or maybe the datatable or maybe even the DataAdapter would have a method/property that can be used to easily determine this!
I know that there several workarounds like looping through all the controls on the form or storing the required information inside the description property of the actual SQL Server field (which what I'm currently doing) but I'm really looking for the most effective way. Looping through lots of controls inside lots of containers etc etc on a regular would be too slow. Also, if I could retrieve the required info from within the exe itself and thereby doing away with all the database access to read the description properties it would be great!