Link to home
Start Free TrialLog in
Avatar of Olukayode Oluwole
Olukayode OluwoleFlag for Canada

asked on

Can i use the same method to load a list and datagrid with the same data

I have a List and a Grid i need to fill  from my database with the same information

The constructor for the JobHistoryModel is shown below:

 public string StaffVariationList
        {
            get
            {
                return $" {Staff_no} {Companyname} {DesignationID} {YearsExp}  { Startdate } { Enddate}  {Companycode}";
            }
        }

The script below  fills the list  (see attached screen)

User generated image
List<JobHistoryModel> variations = GlobalConfig.Connection.GetJobHistoryRecords();
var data = (sender as ListBox).SelectedValue as EmployeeModel;
StaffJobHistoryList.DataSource = variations.Where(variation => data.Staff_no.Equals(variation.Staff_no)).ToList();
variations = variations.OrderBy(x => x.Staff_no).ThenBy(x => x.DesignationID).ToList();

Is it possible for me to do a script similar to above for the Grid  or i need to use a different Method  
to load the Grid

Olukay
Avatar of Kelvin McDaniel
Kelvin McDaniel
Flag of United States of America image

You can do your initial load using a single method, but you'll have to have at least a few separate methods to accomplish what you're trying to do. Essentially, you'll have:
  • A method that gets the central collection of records and stores it somewhere that user events can reach it.
  • A method that queries the central collection and projects the string representations you want to show in the ListView; that will likely be the same method as the one above.
  • A method that queries the central collection for a specific Staff_No and binds that to the Grid
  • A method that handles the change event when the user selects a different row in the list.

If you're not talking about a large or frequently changing dataset then it will work just fine. Otherwise you're going to want to work in a mechanism for periodically refreshing the collection.

I assume this has a WPF or WinForms front end so it'll be easy to implement.

1. Add a protected List<JobHistoryModel> property to your form's logic code. Let's call it Variations (just for ease of reference; you can call it what you want).

2. When the form loads, perform the "master" query and use the results to fill Variations. You should also populate the ListView at this point.

3. In the ListView event (SelectionChanged?), get the Staff_No of the record and use it to query Variations. Use that query result as the datasource for the Grid.

Have fun!
Avatar of Olukayode Oluwole

ASKER

Sounds interesting. I will try this out and get back . When doing this first query where do you
suggest  i keep the records since this can not be the list or grid i want to populate.

Olukay
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for being always helpful

Olukay