Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Defining a Class to Select Instances

Posted on 2016-09-21
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 35

Accepted Solution

ste5an earned 2000 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 35

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?
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 35

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.

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 35

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

688 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