Defining a Class to Select Instances

Posted on 2016-09-21
Last Modified: 2016-09-26
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?
Question by:Declan_Basile
  • 4
  • 3
LVL 33

Accepted Solution

ste5an earned 500 total points
ID: 41810528
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.
LVL 33

Expert Comment

ID: 41811044
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,


Author Comment

ID: 41811526
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 app have a query of a DataTable as a datasource or a filter?
LVL 33

Expert Comment

ID: 41811552
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.
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.


Author Comment

ID: 41811609
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, 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?
LVL 33

Expert Comment

ID: 41812182
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.

Author Comment

ID: 41813073
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)
                    End Using
                End Using

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

Open in new window


Author Closing Comment

ID: 41816041
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.

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In my previous article ( we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now