Solved

Finding the control bound to a specific field in a DataTable

Posted on 2007-03-19
4
251 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
  • 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

706 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now