Solved

ListBox To ListBox Drag Drop And Dataset?

Posted on 2009-05-19
2
313 Views
Last Modified: 2012-06-27
I've found many good articles concerning ListBox drag-n-drop, but I'm still unsure how to update each ListBox's datasource. In this case each Listbox needs to connect to it's own dataset. Each datset will contain 4 columns, one of which is displayed in the ListBox. Upon drag-n-drop, the entire row needs transfered to the opposite dataset. I need to move data back-n-forth as well, not just one-way.

ListBox1 (displays only FirstLastName)  
ds1.tb1 [ID, FirstLastName, Mid, Bday, SS] (entire row moved to other dataset)

ListBox2 (displays only FirstLastName)  
ds2.tb2 [ID, FirstLastName, Mid, Bday, SS]
0
Comment
Question by:pointeman
2 Comments
 
LVL 12

Accepted Solution

by:
wht1986 earned 500 total points
ID: 24427982
In the code below, I have 2 listboxes bound to 2 different datasets (but same schema)

To start with I fill one dataset with values from the database.

The code below shows how I can drag and drop items between the listboxes while moving the associated datarow from 1 dataset table to another.
namespace Win_Forms
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            this.facilitiesTableAdapter.Fill(this.ds1.Facilities);          
        }
 
        private void listBox1_DragDrop(object sender, DragEventArgs e)
        {
            DataRow frow = (DataRow)(e.Data.GetData(e.Data.GetFormats()[0]));
            ds1.Facilities.ImportRow(frow);
            ds2.Facilities.Rows.Remove(frow);
        }
 
        private void listBox2_DragDrop(object sender, DragEventArgs e)
        {
            DataRow frow = (DataRow)(e.Data.GetData(e.Data.GetFormats()[0]));
            ds2.Facilities.ImportRow(frow);
            ds1.Facilities.Rows.Remove(frow);
        }
 
        private void listBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (listBox1.Items.Count == 0)
                return;
            int index = listBox1.IndexFromPoint(e.X, e.Y);
            DoDragDrop(ds1.Facilities[index], DragDropEffects.All);
        }
 
        private void listBox2_MouseDown(object sender, MouseEventArgs e)
        {
            if (listBox2.Items.Count == 0)
                return;
 
            int index = listBox2.IndexFromPoint(e.X, e.Y);
            DoDragDrop(ds2.Facilities[index], DragDropEffects.All);
        }
 
        private void listBox1_DragOver(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.All;
        }
 
        private void listBox2_DragOver(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.All;
        }
    }
}

Open in new window

0
 

Author Closing Comment

by:pointeman
ID: 31583265
Excellent, thanks.........
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

821 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