Visual Basic Sharing data sets from two forms

johbru
johbru used Ask the Experts™
on
My project has two forms:

Form A - is an unbound listview which displays records from DataSet DS.
Form B - Contains Textbox input fields which will be used to update and add records to DS and then display them in listview on form A when form B is closed.

User presses command buttons on Form A to display Form B and add/edit records:
cmdAddButton to add new records
cmdEditButton to edit and update record.

My question is: Can I bind the DS to the Listview on Form A and then bind the Textbox Fields on Form B to the data set used in Listview on Form A. Also how do I display the added/edited items to the Listview on Form A when Form B is closed.

Presently I use a TableAdapter.FillBy to update table in dataset and a bindingsource bound to navigationbar to traverse through the Listview. My listview is unbound and I use the same binding source to load the listview in a separate subroutine.

I'm presently passing the listview to Form B as a new public shared control and then after form B is closed copying it back to the listyview on Form A. To update the underlying tables of ther data set on Form A I'm using Add or Update SQL  in Form B. I would like to just add and update using the Dataset/Bndingsource and Table adapter. Please help with any suggestions

I have included some minor source code to demonstrate that I have coded all this in my solution (it has taken me 2 days), but I know that there is a much better way to do this. I'm a VB4,5,6 programmer and I have tried to use what Visual Studio 2008 offers but I have a very difficult time. I'm just so used to plain coding SQL statement and sending them to server and using unbound controls. Can someone help here. I'm very greatful for any suggestions.
Here is how I pass the listview from Form A to FormB update it and pass it back.
 
Private dim frmLV as listview
public dim sharedLV as New listview
 
   'Get shared_LV to be the same as the Form A Listview
   shared_LV = Me.frmA_LV
   Dim frm as new FormB
 
    If viewIndex = VIEW_SINGLE Then
      frm.WindowState = FormWindowState.Normal
      frm.ShowDialog()
    Else
      If Contains(userForm) Then Exit Sub
      frm.MdiParent = Me
      frm.WindowState = FormWindowState.Maximized
      frm.Show()
    End If
 
   'To shows changes made to shared_LV in FormB input/update form.
   Me.frmA_LV = shared_LV
 
   'Now to get the DataSet to reflect new or updated info
    Me.TradeHistoryTableAdapter.FillByDate(Me.AlpheusDataSet.tradeHistory, showHistory, fromDate, toDate)

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
A datagridview can be bound to a dataset, but to my opinion this is not possible using a listview.

With a little bit of extra work you can iterate through the columns in the dataset and add them to the listview.
Using the same approach, all the records can be added to the listview.

        ListView1.Clear()
 
        Dim col As DataColumn
        Dim row As DataRow
        Dim item As ListViewItem
 
        For Each col In DS.Tables("table").Columns
            ListView1.Columns.Add(col.Caption)
        Next
        For Each row In DS.Tables("table").Rows
            item = New ListViewItem(New String() {row.Item(0), row.Item(1), row.Item(2)})
            ListView1.Items.Add(item)
        Next
 
        ListView1.View = View.Details

Open in new window

I seem to have missed the point of your question, as you indicate that your listview indeed is bound to a dataset. Can you show me how you have accomplished this ?

If you want to use a binding source to edit fields inside of a dataset, you could use

position = Table1BindingSource.Find("fieldname", "value")

to find the position of an element which fieldname has the indicated value (analog to SELECT id FROM table WHERE fieldname='value')

the record itself can then be edited using

record = Table1BindingSource.Item(position)
record.BeginEdit()
record.Item("fieldname") = "updated value"
record.EndEdit()

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial