Sorry but Repost again - Databindings

M

MadCrazyNewbie

Hey Group,

Sorry but i`ve been searching everywhere for this even went out and bought a
ADO book and still carn`t find what im looking for:(

I have the following code for my postion changed:

Private Sub objdsAccess_PositionChanged()
If Me.BindingContext(objdsAccess, "Users").Position <> -1 Then
Me.ComboBox1.SelectedValue =
objdsAccess.Users.Rows(Me.BindingContext(objdsAccess,
"Users").Position).Item("AccessLevelID")
End If
Me.lblNavLocation.Text = (((Me.BindingContext(objdsAccess,
"Users").Position + 1).ToString + " of ") + Me.BindingContext(objdsAccess,
"Users").Count.ToString)
End Sub

This works fine, with my Combo Box. My ComboBox is using the following code:

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
If Me.BindingContext(objdsAccess, "Users").Position <> -1 And Not
mlLoading Then
objdsAccess.Users.Rows(Me.BindingContext(objdsAccess,
"Users").Position).Item("AccessLevelID") = Me.ComboBox1.SelectedValue
End If
End Sub

This all works fine, if im using Navigation buttons and calling:
Me.objdsAccess_PositionChanged().

However I`ve now added a Datagrid to my Form, as I select a Row in my form.
How would i get it to update my ComboBox, and notice the position changed?

Thanks
MCN
 
N

Nathan

From what I can gather, you have a form with a combobox, textboxes, and
navigation buttons that work fine. Then you throw a datagrid on the form and
can't get it to work. Does the datagrid show the same information as the
textboxes? What's the relationship between the info in the cbobox and the
textboxes/datagrid - are they part of the same table or two tables with a
one-to-many relationship?

I'm a newbie to ADO.NET as well, but I may have a solution. Just want to be
sure I understand the problem correctly.
 
M

MadCrazyNewbie

Hi Nathan,

Yes I have a Form, with Text boxs on and a ComboBox. My Combo Box Changes to
the correct value whilst using my nav buttons, but if i select a row in my
datagrid, the text boxs change but my combo box dosn`t, it seems to just
stick on the last selected value. The datagrid has the same info as the
textboxs. My ComboBox looks at my AccessLevels Row in my AccessLevels table
in my database which is bound to my UsersTable.

Many Thanks
MCN
 
N

Nathan

As I said, I'm new at this too, but this is what I've learned to do and it's
a piece of cake. I hope I've understood you correctly. Take a look at the
CurrencyManager--it makes working with databound controls really easy.

You have two tables: AccessLevels and Users, which are bound by a
one-to-many relationship. Put two DataAdapters on the form that connect to
these and generate tables in the same dataset (dsUsers). Open the dataset
schema and create a relationship between the two tables there. Take note of
the name of the relationship, which you can call relAccessLevelsUsers

Then bind the combobox to the AccessLevels table, for example:
DataSource: dsUsers
DataMember: AccessLevels.AccessLevelName

Then bind the DataGrid to the relationship:
DatSource: dsUsers
DataMember: AccessLevels.relAccessLevelsUsers

Also bind the textboxes to a field using the relationship:
Text (under DataBindings): dsUsers - relAccessLevlsUsers.[FieldName]

Then before any other code in the form put:
\\
Dim cmAccessLevels as CurrencyManager
Dim cmUsers as CurrencyManager
\\

And in the Form_Load event:
\\
cmAccessLevels = CType(BindingContext(dsUsers, "AccessLevels"),
CurrencyManager)
cmUsers = CType(BindingContext(dsUsers, "Users"), CurrencyManager)
\\

The CurrencyManager then controls your navigation and editing of your
dataset. Use "Position" property to locate or set the current row of the
AccessLevels table::
\\
Private Sub btnNextAccessLevel_Click(. . .) Handles btnNextAccessLevel.Click
cmAccessLevels.Position +=1
End Sub

Private Sub btnPreviousAccessLevel_Click(. . .) Handles
btnPreviousAccessLevel.Click
cmAccessLevels.Position -=1
End Sub
\\

That code alone will allow you to navigate through the values in the
combobox with the navigation buttons, and the textbox / datagrid should
display only the users related to the current access level. The name in the
cbobox should also change. Select a different item in the cbobox and it
should produce the same effect.

I have a simple project I did for myself that I can email you if you'd like.
I found David Sceppa's book "Microsoft ADO.NET Core Reference" to be of
great help to me.

Let me know if I can make anything more clear.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Sorry to Re-Post - Databindings problem 11
Another Datagrid thing 1
Dataset NOT Updating 15
Newbie - Datagrid Binding Problem 3
Anoying Problem 6
Null & DBNULL Problems 4
DataRow - How To? 13
DBNUL Errors Continued 4

Top