In this article I will explain how to drag and drop from a DataGridView control to a Listbox control
Introduction
In this article I will explain how to drag selected rows from a DataGridView control onto a ListBox control in a Windows Forms (WinForms) application using C#
Step 1
Create a new Windows Forms Application and add a DataGridView and a ListBox control on the form.
Step 2
Write the following in the form load event to populate data in the DataGridView. AllowUserToAddRows is set to false to disable the option to add rows as it is not required. An asterisk (*) is displayed in the row header of the last row of the DataGridView if it is by default set to true:
private void Form3_Load(object sender, EventArgs e)
{
string ConString = @"Data Source=DEEPAK\SQLSERVER2005;Initial Catalog=Employees;User ID=sa;Password=******";
SqlConnection con = new SqlConnection(ConString);
using (SqlDataAdapter sda = new SqlDataAdapter("SELECT DepartmentID, Name FROM Departments", con))
{
DataSet ds = new DataSet();
sda.Fill(ds);
if (ds.Tables.Count > 0)
{
dataGridView1.DataSource = ds.Tables[0].DefaultView;
}
}
dataGridView1.AllowUserToAddRows = false;
}
Step 3
Set the AllowDrop property of the ListBox to true, as in:
listBox1.AllowDrop = true;
The AllowDrop property is used to enable or disable users to drop data onto this control.
Step 4
- Write the following in the MouseDown event of the DataGridView:
private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
dataGridView1.DoDragDrop(dataGridView1.SelectedRows, DragDropEffects.Move);
}
The DoDragDrop method of a control is used to start a drag and drop operation. We call it from MouseDown event of the DataGridView. The first parameter is the data that we want to send in drag and drop operation. Here we are sending selected rows of the DataGridView. The second parameter is a DragDropEffects enumeration that provides the drag and drop operation effect. The cursor style changes accordingly while the drag and drop is being performed. Possible values are DragDropEffects.All, DragDropEffects.Copy, DragDropEffects.Link, DragDropEffects.Move, DragDropEffects.None and DragDropEffects.Scroll.
- Write the following in the DragEnter event of the ListBox:
private void listBox1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(DataGridViewSelectedRowCollection)))
{
e.Effect = DragDropEffects.Move;
}
}
The DragEnter event is fired when an object is dragged into the control's area. The DragEventArgs.Data.GetDataPresent() method is used to check if the dragged data is a type of DataGridViewSelectedRowCollection. Then the drag effect is set using the DragEventArgs.Effect property. It is set to DragDropEffects.Move to show the dragged operation as move.
- Write the following in the DragDrop event of the ListBox:
private void listBox1_DragDrop(object sender, DragEventArgs e)
{
DataGridViewSelectedRowCollection rows = (DataGridViewSelectedRowCollection)e.Data.GetData(typeof(DataGridViewSelectedRowCollection));
foreach (DataGridViewRow row in rows)
{
listBox1.Items.Add(row.Cells[1].Value);
dataGridView1.Rows.Remove(row);
}
}
The DragDrop event is fired when a drag and drop operation is finished. Here, selected rows of the DataGridView is returned into a DataGridViewSelectedRowCollection variable using the DragEventArgs.Data.GetData() method. Then a foreach loop is used to add rows of this collection to items of the ListBox using the ListBox.Items.Add() method. Finally that row is removed from the DataGridView using the DataGridView.Rows.Remove() method.
Conclusion
You can select a row of the DataGridView by clicking on the row header and drag it from the ListBox. This row is removed from the DatGridView and added to the ListBox. To select more than one row you can press the Ctl key while selecting To select all the rows click on the top left blank cell.