Solved

Finding the control bound to a specific field in a DataTable

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

947 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

22 Experts available now in Live!

Get 1:1 Help Now