Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Iterate through a forms binding sources

Status
Not open for further replies.

sillysod

Technical User
Jan 6, 2004
300
GB
I need to cycle through all the binding sources on a form and get the .current for each.

After a fair bit of googling i found a code sample which shows you to cycle though the components.components collection and direct cast the objects to a binding source type.


This is fine if the code is called from within the form using my.components.components

however i need to call the code from within in a class which has a property of type Form

If i then try to access the forms components collection it isnt there?

Does anyone have any ideas?
 
If it is set to Form and you are not passing the information of the actual form then no it isn't going to exist. Either through the classes New() or through a property you would have to set the form or the forms components.components collection.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
So the components dont exist until an actual form is created?

if i used code like the following

dim f as Form
f = someinstance

for each c as control in f.controls
'do whatever
next

There must be someway of doing the same with components?
 
So the components dont exist until an actual form is created?
Kind of, but that isn't what I was saying. You said "however i need to call the code from within in a class which has a property of type Form" You didn't say "I then pass a reference to the form".

Code:
dim f as Form 
f = someinstance

for each c as control in f.controls
'do whatever
next
Though really in a class you would want to do it another way if you did it that way then it should still work. Now, how the binding source exists is different depending on if someinstance is loaded or not. What are you doing in the "do whatever"?

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
ah sorry yea the reference is being passed.

Basically i want to get the bindingsource's current datarow and extract a field value which i will then pass to crystal reports.

The way i do it at the moment is have a sub in about 20 forms and reference the binding source by name in each but maintaining the 20 forms is becoming a nightmare
 
Basically i want to get the bindingsource's current datarow and extract a field value which i will then pass to crystal reports.
I thought you might say that. Then the answer to this
So the components dont exist until an actual form is created?
is that the components exists, but they have no data. If the form isn't loaded the binding source exists, but it has no value. Basically the form knows where to get the data, but has not done so yet. I will have to admit there could be something I missed because I prefer working with unbound controls and only work with bound controls infrequently, but in my experience that has been the case.

The way i do it at the moment is have a sub in about 20 forms and reference the binding source by name in each but maintaining the 20 forms is becoming a nightmare
Ok....You might want to explain everything you are doing because I couldn't see why in any program you would need to maintain 20 forms.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
i might have misled you here,

the someinstance i mentioned above is a reference to a loaded form which has all the dataloaded.

So if i cycled through the controls like so

for each c as control in f.controls
'do whatever
next

i could access the data in those controls.

however i need to access the underlying datarow since there might be a field which is not shown on the form but i need to pass to the report as a parameter (ie the primary key)


re maintaining 20 forms what i mean by that is i have around 20 forms for viewing / adding different records.

however in the db there is a table called tblReports which holds the location of a crystal report and details about what to pass to the crystal report parameters

eg the report might have a parameter called "OrderNumber" and in tblReports i say to pass the field "ID" from the bindingsource.current.

Now this function is common to all the forms however due to the problem above i currently have to copy the code to each form and change the "RunReport" function to be specific to that form. Everything works fine but if i have to change that function i have to reflect the changes across all 20 forms...

The bit thats causing me grief is the following snippet

Code:
Select Case strParValue.Substring(0, 1)
     Case "<"
           'no binding
           GoTo Next_Item
     Case "!"
           'Field Value
            Dim strTable As String = strParValue.Substring
           (1, strParValue.IndexOf(".") - 1)
                        
           Dim strField As String = strParValue.Substring
                        (strParValue.IndexOf(".") + 1,  
                         strParValue.IndexOf("(") - 
                        (strParValue.IndexOf(".") + 1))


          Select Case strTable
            Case "tblJobs"
            Dim jr As MyDataSet.tblJobsRow
            jr = CType(CType(Me.TblJobsBindingSource.Current, DataRowView).Row, IFAtechStockDataSet.tblJobsRow)
            strParValue = jr.Item(strField).ToString
         End Select

       Case "#"
         'Absolute Value
         strParValue = strParValue.Remove(0, 1)
  End Select

The code "Select Case strTable" needs changing on every form (some forms have more than one table)

Hopefully you can see what im trying to achieve a little clearer?
 
First I want to comment on this:
re maintaining 20 forms what i mean by that is i have around 20 forms for viewing / adding different records.
You can't say something is always the case, but generally you shouldn't need that many actual variations on a form. All you do is create the minimum number of forms (For grins we will say 3. One control form and 2 variations of your table forms.) and then all you do is as you need a form is create a new instance of one of those two forms. You can create 20 instances of a form, but then you are only really maintaining 2 forms. That also means you don't have to use a class to iterate through the controls you have your one sub on the form that does that as if you put it on all 20 forms.

As to your other question I'll have to see. I've never tried returning the current row before.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
You really almost have it in your current code.

Code:
Dim CurrentRow as DataRowView = Me.TblJobsBindingSource.Current
strParValue = CurrentRow.Item(strField)


-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top