• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 91
  • Last Modified:

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?
0
Declan_Basile
Asked:
Declan_Basile
  • 4
  • 3
1 Solution
 
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
 
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now