Moving Items Between Two List Boxes, using LLBLGen?

Posts   
 
    
iDave avatar
iDave
User
Posts: 67
Joined: 14-Jun-2010
# Posted on: 14-Jun-2010 07:59:09   

Hallo everyone,

I'm wondering what's the efficient way for "Moving Items Between Two List Boxes" using LLBLGen?

Example of what I have:

On the left listbox I have a list of staff members (StaffEntity) which are filled depending on the combobox selection which is a list of branches (BranchEntity).

On the right listbox, the list of staff members of the newly created branch. Of cource it's empty.

The user selects an item or items (staff) from the leftListBox and move it to the rightListBox. In code, I only need to change the Staff.BranchId of the rightListBox with the newly created branch and save the changes.

So, again, my question, what's the best way of achieving this efficiently?

PS My apologies for my bad english

Thanks in advance!

Regards David De Smet

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 14-Jun-2010 08:27:02   

Well it depends on your interface and how you fetch the entities, and whether they are kept in memory or not.

If the entities are kept in memory, you can loop on the selected items, set the branchId, and then refresh the binding of the target ListBox, to show the added/moved items.

You might also need to refresh the binding of the source ListBox, if you need to filter the items, in order not to show the already added ones.

iDave avatar
iDave
User
Posts: 67
Joined: 14-Jun-2010
# Posted on: 14-Jun-2010 09:13:30   

Walaa wrote:

If the entities are kept in memory, you can loop on the selected items, set the branchId, and then refresh the binding of the target ListBox, to show the added/moved items.

You might also need to refresh the binding of the source ListBox, if you need to filter the items, in order not to show the already added ones.

Would you be so kind to show me how you will do it in code, please?

Thanks for your quick reply!

Regards, David De Smet

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 14-Jun-2010 14:13:24   

I'm sorry but we can do your job for you. We are here to support you do your job. So try and follow my previous message and then if you faced problems, post the code that you have tried and describe the problem you are facing and we will do our best to help you.

iDave avatar
iDave
User
Posts: 67
Joined: 14-Jun-2010
# Posted on: 08-Jul-2010 15:05:40   

Dear Walaa,

My apologies for the late reply. I entirely forgot about the thread.

Anyway, back to the subject...

I think I should have posted my code from the beginning to avoid misunderstandings.

This is my code:


private void TransferStaff(ListBoxControl lvCtrlSource, ListBoxControl lvCtrlTarget, BranchEntity targetBranch)
{
    if (lvCtrlSource.SelectedItems.Count == 0) return;

    _newMySqlConnString = MySqlPwdCipher.DecryptMySqlPwd(RegistrySettings.MySqlConnection, RegistrySettings.MySqlPwd);

    if (string.IsNullOrEmpty(_newMySqlConnString)) return;

    lvCtrlSource.BeginUpdate();
    lvCtrlTarget.BeginUpdate();

    for (int i = 0; i < lvCtrlSource.SelectedItems.Count; i++ )
    {
        var staff = (StaffEntity)lvCtrlSource.SelectedItems[i];
        staff.IsNew = false;
        staff.BranchId = targetBranch.BranchId;

        using (var adapter = new DataAccessAdapter(_newMySqlConnString))
        {
            adapter.SaveEntity(staff);
        }

        Common.Data.GetStaffList(lvCtrlTarget, targetBranch);
        Common.Data.GetStaffList(lvCtrlSource, (BranchEntity)cbBranch.GetSelectedDataRow());
    }

    lvCtrlTarget.EndUpdate();
    lvCtrlSource.EndUpdate();
}

The above code works without any problem. I was just wondering if my code has some performance issue. But I think I'm going to need the adecuate tools to get that information.

Regards, David De Smet

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 08-Jul-2010 17:53:04   

Your code looks fine - the only possible issue is that you save to the database each time the user moves an item from one list to the other.

The more usual approach on a form like this would be to gather all of the changes in memory, and have one database action at the end to commit all of the changes in one go.

Matt