locked
Coding "Add new" button on Binding Navigator RRS feed

  • Question

  •  

    Hello Community,

     

       I have a form with a Tab Control and (4) tabs. Each tabs page has a different table with "Details" view from a databsae (MS Access DB) called "Products" with four tables. When I add the details view form the first table to tab1, it adds a Binding Navigator for that table. As I add the other three tables to the remaining tabs, it does not add a Binding Navigator for each additional table. My delima is that I need a Binding Navigator for each table of each tab. I can add a Binding Navigator from the ToolBox but it does not automatically add functionality to add, delete, and save records to the table in the tabs page.

     

       My question to the forum community is what code would I use to program the "add new" record to the Binding Navigator(s) so that they will communicate with the table in the tabs page? I assume I could also use Buttons on each tab page, but a Binding Navigator would work best. I know how to add "save" and "delete" code.

     

    Here is my database details:

       Database name= Products

       table1 = Protein

       table2 = Liquid

       table3 = Capsule

       table4 = Powder

     

    each table has the following same fields:

    - productID

    - productName

    - size

    - cost

    - amountInStock

     

    Thanks

    DPS Officer

    Tuesday, September 9, 2008 1:03 AM

Answers

  • Hi Preston,

     

    Keep in mind that anything you can do in the designer can be done in code.  You can access the same properties in code as the properties window shows.  The difference is that sometimes it is handled somewhat differently in the code because for instance with the databindings for a textbox (details view), in the designer you can just set the text property to a column of a table.  This will add the binding for you.  In code you need to use the databindings.add method and it has to be entered as string values for some of the objects and properties.  In this example it shows how to add the databinding for a textbox to a bindingsource and a specific column.  It also shows how to create all the objects needed to do what you are doing in the designer with the exception of the dataset.  This just uses a datatable that is filled with the data from the database.  In the designer you are also filling a datatable but it is a part of a dataset that is created.  You don't need a dataset in many cases.  The table is what you want to work with.  Adding a dataset will also allow you to add tables by name and you can refer to them by name as well but you can do the same with a declared datatable.  I have seen some advantages to using a dataset when using the debugger though.  When using the debugger setting breakpoints and then selecting your dataset object in the code you can load up the tables in the designer and see the tables and the data.  It can help in certain cases.  Ok here is the example

     

    Dim exe_location As String = Application.StartupPath

        Public PS_Parking_connection As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & exe_location & "\PS_Parking.mdb;Persist Security Info=True")

        Public Add_User_string As String = String.Empty

        Public Add_User_command As OleDb.OleDbCommand

        Public Add_User_dataadapter As OleDb.OleDbDataAdapter

        Public Add_User_commandbuilder As OleDb.OleDbCommandBuilder  'command builder can be used when you call the update method

        Public Add_User_table As New DataTable

        Public Add_User_bindingsource As New BindingSource

     

     

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

     

            get_Add_User_records()

            Me.TextBox1.DataBindings.Add("Text", Me.Table1BindingSource, "your column name")

     'here is where textbox 1 text property is bound to a bindingsource and a specific column.

        End Sub

     

        Private Sub get_Add_User_records()

            Add_User_string = "SELECT * FROM Add_User"

            Add_User_command = New OleDb.OleDbCommand(Add_User_string, PS_Parking_connection)

            Add_User_dataadapter = New OleDb.OleDbDataAdapter(Add_User_command)

            Add_User_command.Connection.Open()

            Add_User_dataadapter.Fill(Add_User_table)

            Add_User_bindingsource.DataSource = Add_User_table

            Add_User_command.Connection.Close()

            Me.Add_UserBindingNavigator.BindingSource = Add_User_bindingsource

        End Sub

     

    you can also take advantage of the designer generated objects such as your bindingsource and work with it in code as well. 

     

    Hope this helps

    Jeff

    Tuesday, September 9, 2008 5:53 PM

All replies

  • Hi Preston,

     

    I am not sure why it would not add the navigator for each table on the tabs if you drag from the designer.  I don't use the designer as i am sure you remember.  But the navigator needs to be tied to your bindingsource such as your parkingviolations binding source.  And your bindingsource needs to be tied to your dataset table.  Once you hook these items up then it will work for you.  If you need to you can also code the add new button by using the addnew method of your bindingsource for that particular table on that particular tab.  If you go back and look closely at the search_parking_violations form in your public safety project you will see how the navigator, bindingsource and dataset tables are hooked together.  Look at the datasources and the datamembers.  The datasource is how you hookup the objects together and the datamember will be to set your bindingsource to the correct table in the dataset.  Otherwise you can do it all in code.  I find it easier to just code it but in know that your project most likely is drag and drop.  Let me know if this helps you get it figured out.  If not i can post a sample to hook everything up in code.

     

    Jeff

    Tuesday, September 9, 2008 2:19 AM
  • Hello jeff,

     

       I must say that I am impressed that you remeber back to my past work with the Parking Violations form. Very Good!

     

    If you could post some code for me, that would be awsome. I prefer to look at working code since I'm not as familar with the method in which you just described. I'm learning slowly and since my job is "Two Sided" where I work as an Officer and Programmer, I don't get as much experience to research and learn new things in programming as I would like.

     

     

    Thanks,

    Preston

     

    Tuesday, September 9, 2008 2:40 AM
  • Hello Jeff,

     

       I figured out what I should have been doing and wasn't. I add a Binding Navigator to each tab page and in the Binding Navigator properties I have to add the Biding Source to each table by clicking the empty text box to the Binding Source in the properties view and selecting the table. This is only available after drag and dropping the details view of each table to the tab page and then it will display a selection of tables to pick from.

     

    I would still be very interested in seeing the what you could also show me in code.

     

     

    Preston

     

    Tuesday, September 9, 2008 2:54 AM
  • Hi Preston,

     

    Keep in mind that anything you can do in the designer can be done in code.  You can access the same properties in code as the properties window shows.  The difference is that sometimes it is handled somewhat differently in the code because for instance with the databindings for a textbox (details view), in the designer you can just set the text property to a column of a table.  This will add the binding for you.  In code you need to use the databindings.add method and it has to be entered as string values for some of the objects and properties.  In this example it shows how to add the databinding for a textbox to a bindingsource and a specific column.  It also shows how to create all the objects needed to do what you are doing in the designer with the exception of the dataset.  This just uses a datatable that is filled with the data from the database.  In the designer you are also filling a datatable but it is a part of a dataset that is created.  You don't need a dataset in many cases.  The table is what you want to work with.  Adding a dataset will also allow you to add tables by name and you can refer to them by name as well but you can do the same with a declared datatable.  I have seen some advantages to using a dataset when using the debugger though.  When using the debugger setting breakpoints and then selecting your dataset object in the code you can load up the tables in the designer and see the tables and the data.  It can help in certain cases.  Ok here is the example

     

    Dim exe_location As String = Application.StartupPath

        Public PS_Parking_connection As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & exe_location & "\PS_Parking.mdb;Persist Security Info=True")

        Public Add_User_string As String = String.Empty

        Public Add_User_command As OleDb.OleDbCommand

        Public Add_User_dataadapter As OleDb.OleDbDataAdapter

        Public Add_User_commandbuilder As OleDb.OleDbCommandBuilder  'command builder can be used when you call the update method

        Public Add_User_table As New DataTable

        Public Add_User_bindingsource As New BindingSource

     

     

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

     

            get_Add_User_records()

            Me.TextBox1.DataBindings.Add("Text", Me.Table1BindingSource, "your column name")

     'here is where textbox 1 text property is bound to a bindingsource and a specific column.

        End Sub

     

        Private Sub get_Add_User_records()

            Add_User_string = "SELECT * FROM Add_User"

            Add_User_command = New OleDb.OleDbCommand(Add_User_string, PS_Parking_connection)

            Add_User_dataadapter = New OleDb.OleDbDataAdapter(Add_User_command)

            Add_User_command.Connection.Open()

            Add_User_dataadapter.Fill(Add_User_table)

            Add_User_bindingsource.DataSource = Add_User_table

            Add_User_command.Connection.Close()

            Me.Add_UserBindingNavigator.BindingSource = Add_User_bindingsource

        End Sub

     

    you can also take advantage of the designer generated objects such as your bindingsource and work with it in code as well. 

     

    Hope this helps

    Jeff

    Tuesday, September 9, 2008 5:53 PM