By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,364 Members | 1,254 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,364 IT Pros & Developers. It's quick & easy.

VS2005,VB,BindingNavigator: programmatically moving to record

P: n/a
As you can tell, I am new to VS.net.

I have a VB project with a defined data source pointing to a table in a ..mdb file.-- The associated fields are displayed in textboxes using the tableBindingSource. There is also an associated BindingNavigator. The table's primary key is a field called 'who'. I have a valid value for 'who' and want to set the Navigator to the record that contains this value so the fields are displayed in the associated textboxes. The dataset is loaded with all records during FormLoad event.

The program's tray contains DataSettable, tableBindingSource, tableTableAdapter, BindingNavigator.

I tried tableBindingSource.Find("who", swho) - this doesn't seem to do anything. swho is a string containing a known valid value.

How do I accomplish this?

Also, if data is edited in the textboxes, I want to save the changes to the mdb file - how do I do that?

Same with Adding & Deleting records.

One other question - var = BindingNavigator.MoveNextItem does not advance the Navigator. How to do this?

Appreciate the help ... Thank you.

Dale Sampson

Nov 4 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
This is a lot of questions with complex answers.
I'll take a stab at some of them.

The find method returns an integer that points
to the position of the record you're looking for.
If it finds it, you need to change position to
that record.

Dim index as Integer = myBindingSource.Find("field",txtField.Text)
If Index <-1 Then
'it was found, change position to that record
myBindingSource.Position = index
End If

On your binding navigator, if the buttons contained
therein don't work, make sure the [DataSource]
property is filled in and points to the right data source.

As for updating the database, it depends on if you're
using stored procedures, writing your own queries, etc.
Changes made on the screen will change the underlying
dataset, but will not be written back to the original
data source unless you specifically write that code.

As for positioning, I don't think you can invoke the
methods on the binding navigator. Instead of doing
that, modify the position property of the binding
source:

myBindingSource.Position += 1

or just do this: myBindingSource.MoveNext

I recommend that you check out "Data Binding with
Windows Forms 2.0" by Brian Noyes. It's written for
C#, but the downloadable code is available in both
C# and VB.

Good luck. Hope this helps.

Robin S.
"Dale Sampson" <da********@newsgroups.nospamwrote in message news:ef**************@TK2MSFTNGP04.phx.gbl...
As you can tell, I am new to VS.net.

I have a VB project with a defined data source pointing to a table in a .mdb file.-- The associated fields are displayed in textboxes using the tableBindingSource. There is also an associated BindingNavigator. The table's primary key is a field called 'who'. I have a valid value for 'who' and want to set the Navigator to the record that contains this value so the fields are displayed in the associated textboxes. The dataset is loaded with all records during FormLoad event.

The program's tray contains DataSettable, tableBindingSource, tableTableAdapter, BindingNavigator.

I tried tableBindingSource.Find("who", swho) - this doesn't seem to do anything. swho is a string containing a known valid value.

How do I accomplish this?

Also, if data is edited in the textboxes, I want to save the changes to the mdb file - how do I do that?

Same with Adding & Deleting records.

One other question - var = BindingNavigator.MoveNextItem does not advance the Navigator. How to do this?

Appreciate the help ... Thank you.

Dale Sampson

Nov 6 '06 #2

P: n/a
Hi Dale,

Firstly, a common walkthrough of using DataSet, BindingSource and
BindingNavigator in a WinForms application is like below.

1. Add DataSet into the project. To do this, go to Project|Add New Item
menu and select DataSet in the 'Add New Item' window.

2. Add a connection to the database you'd like to use in the Server
Explorer. Scroll to the table you want to use in the Server Explorer and
drag it onto the DataSet in the designer. This will add a DataTable and a
corresponding TableAdapter (you could use the auto-generated TableAdapter
to retrieve data from DB into DataTable and save the data back to DB).

3. Add an instance of the DataSet, BindingSource and BindingNavigator onto
the form. Add some controls onto the form to display data.

4. Set the BindingSource instance's DataSource property to the DataSet
instance and the BindingSource instance's DataMember property to the
datatable name.

5. Set the BindingNavigator instance's BindingSource property to the
BindingSource instance.

6. Bind the controls on the form to the BindingSource instance.

7. Fill the DataSet in the form's Load event handler.

Build and run the program. When you click the 'move next' or 'move
previous' button in the BindingNavigator control, the record currently
displayed in the controls is changed. In fact, the BindingNavigator control
navigates the records by changing the Position property of its underlying
BindingSource object. In order to change the current row by code, we set
the Position property of the BindingSource object.

So as for your first question, since you have found out the index of the
row by calling the BindingSource.Find method, you should set the
BindingSource.Position to the index to navigate to this row.

As for your second question, call the BindingSource.EndEdit method to apply
pending changes to the underlying data source first. Then, if the datatable
has a corresponding TableAdapter, you could call the Update method of the
TableAdapter object to save the data back to DB. Otherwise, you need to
write an update SQL statement and execute it using a SqlCommand object to
save the data to DB. I prefer the first option, which is more convenient.

As for your third question, as I have mentioned above, set the Position
property of the BindingSource instance. BindingNavigator.MoveNextItem
returns the 'move next' ToolStripItem.

Hope this helps.
If you have anything unclear, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 6 '06 #3

P: n/a
Hi Dale,

How about the problem now? Is everything clear now?

If you have anything unclear, please feel free to let me know.

Thank you for using our MSDN Managed Newsgroup Support Service!
Sincerely,
Linda Liu
Microsoft Online Community Support

Nov 8 '06 #4

P: n/a
Thank you RobinS & Linda - I appreciate the help.

yes - the info you've provided has helped & I am to navigate the database & position as needed.

A related question.

As you pointed out, edits to the data are not saved until calls to the underlying bindingsource & tableadapter are called. For this purpose, I've added a Save button that does this & added a handler to trap the fields text_changed event to make the user aware of a pending change & enable Save. That works ok.

I haven't figure out a way to handle the case where the user decided NOT to save the changes. E.g., uses move_next _record or does a new search without clicking Save. In this case, I would like to 'roll back' the change i.e., have the original text displayed if the user goes back to the 'edited' but unsaved record. Can you point me to documentation or make suggestions about how I might accomplish this?

Thank you,

--
Dale Sampson
http://www.dalesplace.net
"Dale Sampson" <da********@newsgroups.nospamwrote in message news:ef**************@TK2MSFTNGP04.phx.gbl...
As you can tell, I am new to VS.net.

I have a VB project with a defined data source pointing to a table in a .mdb file.-- The associated fields are displayed in textboxes using the tableBindingSource. There is also an associated BindingNavigator. The table's primary key is a field called 'who'. I have a valid value for 'who' and want to set the Navigator to the record that contains this value so the fields are displayed in the associated textboxes. The dataset is loaded with all records during FormLoad event.

The program's tray contains DataSettable, tableBindingSource, tableTableAdapter, BindingNavigator.

I tried tableBindingSource.Find("who", swho) - this doesn't seem to do anything. swho is a string containing a known valid value.

How do I accomplish this?

Also, if data is edited in the textboxes, I want to save the changes to the mdb file - how do I do that?

Same with Adding & Deleting records.

One other question - var = BindingNavigator.MoveNextItem does not advance the Navigator. How to do this?

Appreciate the help ... Thank you.

Dale Sampson

Nov 8 '06 #5

P: n/a
Hi Dale,

Thank you for your response.

Let's say that we have some controls, e.g. TextBox, ComboBox and etc on the
form to display a row in the data source at one time. If you edit the data
in the controls and move to the next row, the EndEdit method of the
BindingSource is called internally, which applies the pending changes to
the underlying data source.

If you'd like to cancel the changes, you could use the CancelEdit method of
the BindingSource to do it.

If you are using your own buttons to navigate among the rows by changing
the BindingSource instance's Position property, you call the BindingSoruce
instance's CancelEdit method before changing the BindingSource instance's
Position property.

If you are using a BindingNavigator control to do the navigating, you could
derive a new control from BindingNavigator class and override the
OnItemChecked method in the new control to call the CancelEdit method of
BindingSource.

The following is the sample code of the derived class.

Imports System.Windows.Forms

Public Class ExtBindingNavigator
Inherits BindingNavigator

Public Sub New()
End Sub

Public Sub New(ByRef bs As BindingSource)
MyBase.New(bs)
End Sub

Protected Overrides Sub OnItemClicked(ByVal e As
System.Windows.Forms.ToolStripItemClickedEventArgs )

If (e.ClickedItem.Name = Me.MovePreviousItem.Name Or
e.ClickedItem.Name = Me.MoveNextItem.Name Or _
e.ClickedItem.Name = Me.MoveFirstItem.Name Or
e.ClickedItem.Name = Me.MoveLastItem.Name) Then

Me.BindingSource.CancelEdit()
End If

MyBase.OnItemClicked(e)

End Sub

End Class

Build the project and add an instance of the ExtBindingNavigator onto the
form and set its BindingSource property to the BindingSource instance on
the form.

Hope this helps.
If you have anything unclear, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 9 '06 #6

P: n/a
Linda,

Thank you - I'll try this out tomorrow & if any problem, I'll post again.

Dale

"Linda Liu [MSFT]" <v-****@online.microsoft.comwrote in message
news:nI**************@TK2MSFTNGXA01.phx.gbl...
Hi Dale,

Thank you for your response.

Let's say that we have some controls, e.g. TextBox, ComboBox and etc on
the
form to display a row in the data source at one time. If you edit the data
in the controls and move to the next row, the EndEdit method of the
BindingSource is called internally, which applies the pending changes to
the underlying data source.

If you'd like to cancel the changes, you could use the CancelEdit method
of
the BindingSource to do it.

If you are using your own buttons to navigate among the rows by changing
the BindingSource instance's Position property, you call the BindingSoruce
instance's CancelEdit method before changing the BindingSource instance's
Position property.

If you are using a BindingNavigator control to do the navigating, you
could
derive a new control from BindingNavigator class and override the
OnItemChecked method in the new control to call the CancelEdit method of
BindingSource.

The following is the sample code of the derived class.

Imports System.Windows.Forms

Public Class ExtBindingNavigator
Inherits BindingNavigator

Public Sub New()
End Sub

Public Sub New(ByRef bs As BindingSource)
MyBase.New(bs)
End Sub

Protected Overrides Sub OnItemClicked(ByVal e As
System.Windows.Forms.ToolStripItemClickedEventArgs )

If (e.ClickedItem.Name = Me.MovePreviousItem.Name Or
e.ClickedItem.Name = Me.MoveNextItem.Name Or _
e.ClickedItem.Name = Me.MoveFirstItem.Name Or
e.ClickedItem.Name = Me.MoveLastItem.Name) Then

Me.BindingSource.CancelEdit()
End If

MyBase.OnItemClicked(e)

End Sub

End Class

Build the project and add an instance of the ExtBindingNavigator onto the
form and set its BindingSource property to the BindingSource instance on
the form.

Hope this helps.
If you have anything unclear, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no
rights.

Nov 9 '06 #7

P: n/a
Linda,

I had a chance to try out your suggestion & it works nicely. You've also
improved my understanding of VB.net & I'll keep in mind using inheritance to
tailor a control's / classes' events.

Just for others info: Public Sub New(ByRef bs As BindingSource) should read
As BindingNavigator

Thank you again for all your help!

Best regards,

--
Dale Sampson
http://www.dalesplace.net

"Linda Liu [MSFT]" <v-****@online.microsoft.comwrote in message
news:nI**************@TK2MSFTNGXA01.phx.gbl...
Hi Dale,

Thank you for your response.

Let's say that we have some controls, e.g. TextBox, ComboBox and etc on
the
form to display a row in the data source at one time. If you edit the data
in the controls and move to the next row, the EndEdit method of the
BindingSource is called internally, which applies the pending changes to
the underlying data source.

If you'd like to cancel the changes, you could use the CancelEdit method
of
the BindingSource to do it.

If you are using your own buttons to navigate among the rows by changing
the BindingSource instance's Position property, you call the BindingSoruce
instance's CancelEdit method before changing the BindingSource instance's
Position property.

If you are using a BindingNavigator control to do the navigating, you
could
derive a new control from BindingNavigator class and override the
OnItemChecked method in the new control to call the CancelEdit method of
BindingSource.

The following is the sample code of the derived class.

Imports System.Windows.Forms

Public Class ExtBindingNavigator
Inherits BindingNavigator

Public Sub New()
End Sub

Public Sub New(ByRef bs As BindingSource)
MyBase.New(bs)
End Sub

Protected Overrides Sub OnItemClicked(ByVal e As
System.Windows.Forms.ToolStripItemClickedEventArgs )

If (e.ClickedItem.Name = Me.MovePreviousItem.Name Or
e.ClickedItem.Name = Me.MoveNextItem.Name Or _
e.ClickedItem.Name = Me.MoveFirstItem.Name Or
e.ClickedItem.Name = Me.MoveLastItem.Name) Then

Me.BindingSource.CancelEdit()
End If

MyBase.OnItemClicked(e)

End Sub

End Class

Build the project and add an instance of the ExtBindingNavigator onto the
form and set its BindingSource property to the BindingSource instance on
the form.

Hope this helps.
If you have anything unclear, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no
rights.

Nov 9 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.