Solved

Defining a Class to Select Instances

Posted on 2016-09-21
8
54 Views
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?
0
Comment
Question by:Declan_Basile
  • 4
  • 3
8 Comments
 
LVL 32

Accepted Solution

by:
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.
0
 
LVL 32

Expert Comment

by:sarabande
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,

Sara
0
 
LVL 1

Author Comment

by:Declan_Basile
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 vb.net app have a query of a DataTable as a datasource or a filter?
0
 
LVL 32

Expert Comment

by:ste5an
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.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 1

Author Comment

by:Declan_Basile
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 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
 
LVL 32

Expert Comment

by:ste5an
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.
0
 
LVL 1

Author Comment

by:Declan_Basile
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)
                        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
 
LVL 1

Author Closing Comment

by:Declan_Basile
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.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This video discusses moving either the default database or any database to a new volume.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

14 Experts available now in Live!

Get 1:1 Help Now