Defining a Class to Select Instances

I want to define a class in which I can open a form with two listboxes side by side.  The user will be able to double click an item in one listbox to move that item to the other listbox.  The listbox to the left will be unselected items, and the listbox to the right will be selected items.  I want to fill a generic recordset with fields "PKid", "instancename", and "selected" and open the form with that recordset.  "Selected" is a Boolean field.  Any records with selected = True will show in the listbox on the right, otherwise the record will show in the listbox on the left.  Double clicking an item will toggle the selected field for that record and requery the listboxes.  In some cases I'll fill the recordset with Employees from the Employees table so users can select a group of Employees, in other cases I'll fill it with Operations from the Operations table so the users can select a group of operations, etc.  When the form is closed by the user I'll have a recordset of elements of which I'll know which ones were selected by the user.  
To accomplish this, would I have a class called "Selection" with a Recordset property and a "SelectInstances" method to open the form?  How would I define the fields in the recordset and fill the recordset with the data from a SQL statement or stored procedure?
LVL 1
Declan_BasileITAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
Take a look at the MVVM pattern. Create you form (view) based on an general view model. Then you need only generic LoadFrom() and SaveTo() function in the view model, to write the actual glue code for the data binding.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sarabandeCommented:
i think it would be better to have 3 layers: database - application - views rather than to load and save directly to/from record views.

that means you have a "model" class which holds an array of application items.  these items are created and put to Array by reading the recordset. your form would read the data from model and initially fill the listboxes. if the user wants to save, the model was updated from the listboxes, i. e. the selected property was updated if an item was moved from left to right or vice versa.only changed items need to get updated in the record set,

Sara
0
Declan_BasileITAuthor Commented:
I've tried reading a couple of articles about MVVM and I'm lost.  Can you explain the mechanics of this (as opposed to the concepts)?  Can I create a DataTable with fields "Id, instanceName, and selected", populate the table with data from a SQL statement and pass the DataTable as an argument to the form.  Can the form then have two listboxes, one with the DataTable as the datasource and a "selected = True" filter and the other with the DataTable as the datasource and a "Selected = False" filter?  Can a listbox in a vb.net app have a query of a DataTable as a datasource or a filter?
0
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

ste5anSenior DeveloperCommented:
The view model is an abstraction layer to support the view. It is not necessary nor appropriate to use a database centric storage. Thus I would use in the view model a List<Item> where Item is a plain C# class (plain old c# object, POCO). This item class should have a key property, a display text property and the selected property. You build your view (form) with these simple classes. The only connection to the real data is a load and a save method which loads the data from your database and a save method to store the changes.
0
Declan_BasileITAuthor Commented:
I just read through code of this pattern:
bindingsource.datasource = datatable
bindingsource.filter = "some filter"
Listbox.Datasource = bindingsource

This looked like a good approach to me, although I don't know yet how to define the fields of the datatable, load the datatable from a SQL statement, and send the datatable as an argument when loading the form.  Also, I'm using vb.net, not c#.

Is there sample code I could look at to do it the way you suggest?  Code that would show me how to define the item class, the collection of items class, the load method that accepts an SQL statement to create the items from, the way to pass the collection of items to the form when its loaded, the way to define the listboxes so one shows items with selected = true and the other shows items with selected = false?
0
ste5anSenior DeveloperCommented:
That is the key in this case with the MVVM model. You don't have to care about it, when you design your view model data source agnostic.
0
Declan_BasileITAuthor Commented:
I just finished getting it to work with the following code.  On the form I have the two listboxes, each with its own databinding and dataview objects.  How would the MVVM model be different than what I did?

                Using cn As New SqlConnection(strConn)
                    SQL = "With R As (Select UserId FROM RelOperationUser WHERE OperationId = " &
                        DataGridView1.Columns(e.ColumnIndex).Tag &
                        ") Select U.UserId As Id, U.FullName As Name, IIf(R.UserId Is Null, 0, 1) As Selected 
                        FROM Users U LEFT JOIN R ON U.UserId = R.UserId WHERE U.Terminated = 0"
                    Using da As New SqlDataAdapter(SQL, cn)
                        da.Fill(dt)
                    End Using
                End Using

                Dim oForm As SelectItems
                oForm = New SelectItems(dt)
                oForm.SetTitle("Select Users For " & DataGridView1.Columns(e.ColumnIndex).HeaderText)
                oForm.ShowDialog()
                oForm = Nothing

Open in new window

0
Declan_BasileITAuthor Commented:
Thanks for the reference to MVVM.  I got it working the best way I know how for now, and as my experience grows I'll probably better understand your recommended way of doing it.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.