Solved

Finding the control bound to a specific field in a DataTable

Posted on 2007-03-19
4
258 Views
Last Modified: 2013-11-07
Hi Experts,

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!

Any ideas?

Thanks
0
Comment
Question by:PantoffelSlippers
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 34

Accepted Solution

by:
Sancler earned 250 total points
ID: 18750353
I think you'll have to loop, but through the bindings collection of the bindingcontext rather than through the controls themselves would be most efficient.  Something like this

        Dim s As String = "<myFieldName>"
        For Each b As Binding In CType(BindingContext(<myTable>), CurrencyManager).Bindings
            Dim bo As BindingMemberInfo = b.BindingMemberInfo
            If bo.BindingField = s Then
                Debug.WriteLine(b.Control.Name)
                Exit For
            End If
        Next

Roger
0
 

Author Comment

by:PantoffelSlippers
ID: 18751266
Thanks Roger

I'll give that a shot.
0
 

Author Comment

by:PantoffelSlippers
ID: 18897454
Roger,

Havent tried yet.  On another project for now.  This is important - will report back soon.

Thanks
0
 

Author Comment

by:PantoffelSlippers
ID: 18999985
Thanks Roger

It works great!

Sorry about the long delay.

All the best and thanks again.
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question