Solved

ListBox To ListBox Drag Drop And Dataset?

Posted on 2009-05-19
2
291 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Problem to open Excel file 15 44
Check my code out(2) 1 33
IO.FileSystemWatcher dies when used in Powershell 1 27
C# TextBox 11 13
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

759 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now