VS2005,VB,BindingNavigator: programmatically moving to record

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dale Sampson

    VS2005,VB,BindingNavigator: programmatically moving to record

    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 tableBindingSou rce. There is also an associated BindingNavigato r. 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, tableBindingSou rce, tableTableAdapt er, BindingNavigato r.

    I tried tableBindingSou rce.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 = BindingNavigato r.MoveNextItem does not advance the Navigator. How to do this?

    Appreciate the help ... Thank you.

    Dale Sampson

  • RobinS

    #2
    Re: VS2005,VB,Bindi ngNavigator: programmaticall y moving to record

    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",t xtField.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" <dalesplace@new sgroups.nospamw rote in message news:efS64cCAHH A.4892@TK2MSFTN GP04.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 tableBindingSou rce. There is also an associated BindingNavigato r. 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, tableBindingSou rce, tableTableAdapt er, BindingNavigato r.

    I tried tableBindingSou rce.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 = BindingNavigato r.MoveNextItem does not advance the Navigator. How to do this?

    Appreciate the help ... Thank you.

    Dale Sampson

    Comment

    • Linda Liu [MSFT]

      #3
      RE: VS2005,VB,Bindi ngNavigator: programmaticall y moving to record

      Hi Dale,

      Firstly, a common walkthrough of using DataSet, BindingSource and
      BindingNavigato r 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 BindingNavigato r 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 BindingNavigato r 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 BindingNavigato r control, the record currently
      displayed in the controls is changed. In fact, the BindingNavigato r 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.F ind method, you should set the
      BindingSource.P osition to the index to navigate to this row.

      As for your second question, call the BindingSource.E ndEdit 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. BindingNavigato r.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
      Learn with interactive lessons and technical documentation, earn professional development hours and certifications, and connect with the community.

      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.

      Comment

      • Linda Liu [MSFT]

        #4
        RE: VS2005,VB,Bindi ngNavigator: programmaticall y moving to record

        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

        Comment

        • Dale Sampson

          #5
          Re: VS2005,VB,Bindi ngNavigator: programmaticall y moving to record

          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

          "Dale Sampson" <dalesplace@new sgroups.nospamw rote in message news:efS64cCAHH A.4892@TK2MSFTN GP04.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 tableBindingSou rce. There is also an associated BindingNavigato r. 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, tableBindingSou rce, tableTableAdapt er, BindingNavigato r.

          I tried tableBindingSou rce.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 = BindingNavigato r.MoveNextItem does not advance the Navigator. How to do this?

          Appreciate the help ... Thank you.

          Dale Sampson

          Comment

          • Linda Liu [MSFT]

            #6
            Re: VS2005,VB,Bindi ngNavigator: programmaticall y moving to record

            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 BindingNavigato r control to do the navigating, you could
            derive a new control from BindingNavigato r 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 ExtBindingNavig ator
            Inherits BindingNavigato r

            Public Sub New()
            End Sub

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

            Protected Overrides Sub OnItemClicked(B yVal e As
            System.Windows. Forms.ToolStrip ItemClickedEven tArgs)

            If (e.ClickedItem. Name = Me.MovePrevious Item.Name Or
            e.ClickedItem.N ame = Me.MoveNextItem .Name Or _
            e.ClickedItem.N ame = Me.MoveFirstIte m.Name Or
            e.ClickedItem.N ame = Me.MoveLastItem .Name) Then

            Me.BindingSourc e.CancelEdit()
            End If

            MyBase.OnItemCl icked(e)

            End Sub

            End Class

            Build the project and add an instance of the ExtBindingNavig ator 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
            Learn with interactive lessons and technical documentation, earn professional development hours and certifications, and connect with the community.

            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.

            Comment

            • Dale Sampson

              #7
              Re: VS2005,VB,Bindi ngNavigator: programmaticall y moving to record

              Linda,

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

              Dale

              "Linda Liu [MSFT]" <v-lliu@online.mic rosoft.comwrote in message
              news:nIaCqE7AHH A.1984@TK2MSFTN GXA01.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 BindingNavigato r control to do the navigating, you
              could
              derive a new control from BindingNavigato r 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 ExtBindingNavig ator
              Inherits BindingNavigato r
              >
              Public Sub New()
              End Sub
              >
              Public Sub New(ByRef bs As BindingSource)
              MyBase.New(bs)
              End Sub
              >
              Protected Overrides Sub OnItemClicked(B yVal e As
              System.Windows. Forms.ToolStrip ItemClickedEven tArgs)
              >
              If (e.ClickedItem. Name = Me.MovePrevious Item.Name Or
              e.ClickedItem.N ame = Me.MoveNextItem .Name Or _
              e.ClickedItem.N ame = Me.MoveFirstIte m.Name Or
              e.ClickedItem.N ame = Me.MoveLastItem .Name) Then
              >
              Me.BindingSourc e.CancelEdit()
              End If
              >
              MyBase.OnItemCl icked(e)
              >
              End Sub
              >
              End Class
              >
              Build the project and add an instance of the ExtBindingNavig ator 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
              Learn with interactive lessons and technical documentation, earn professional development hours and certifications, and connect with the community.

              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.
              >

              Comment

              • Dale Sampson

                #8
                Re: VS2005,VB,Bindi ngNavigator: programmaticall y moving to record

                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 BindingNavigato r

                Thank you again for all your help!

                Best regards,

                --
                Dale Sampson


                "Linda Liu [MSFT]" <v-lliu@online.mic rosoft.comwrote in message
                news:nIaCqE7AHH A.1984@TK2MSFTN GXA01.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 BindingNavigato r control to do the navigating, you
                could
                derive a new control from BindingNavigato r 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 ExtBindingNavig ator
                Inherits BindingNavigato r
                >
                Public Sub New()
                End Sub
                >
                Public Sub New(ByRef bs As BindingSource)
                MyBase.New(bs)
                End Sub
                >
                Protected Overrides Sub OnItemClicked(B yVal e As
                System.Windows. Forms.ToolStrip ItemClickedEven tArgs)
                >
                If (e.ClickedItem. Name = Me.MovePrevious Item.Name Or
                e.ClickedItem.N ame = Me.MoveNextItem .Name Or _
                e.ClickedItem.N ame = Me.MoveFirstIte m.Name Or
                e.ClickedItem.N ame = Me.MoveLastItem .Name) Then
                >
                Me.BindingSourc e.CancelEdit()
                End If
                >
                MyBase.OnItemCl icked(e)
                >
                End Sub
                >
                End Class
                >
                Build the project and add an instance of the ExtBindingNavig ator 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
                Learn with interactive lessons and technical documentation, earn professional development hours and certifications, and connect with the community.

                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.
                >

                Comment

                Working...