Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 338
  • Last Modified:

ListBox To ListBox Drag Drop And Dataset?

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
pointeman
Asked:
pointeman
1 Solution
 
wht1986Commented:
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
 
pointemanAuthor Commented:
Excellent, thanks.........
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

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