Quantcast
Channel: DEEPAK SHARMA » DataGridView
Viewing all articles
Browse latest Browse all 2

Drag and Drop from DataGridView to ListBox in a Windows Forms application

0
0

In this article I will explain how to drag and drop from a DataGridView control to a Listbox control

Download source code

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 following in the form load event to populate data in the DataGridView. AllowUserToAddRows is set to false to disable option to add rows as it is not required. An asterisk (*) is displayed in the row header of 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 AllowDrop property of the ListBox to true

listBox1.AllowDrop = true;

AllowDrop property is used to enable or disable users to drop data on this control

Step 3:

  • Write following in MouseDown event of the DataGridView
private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
     dataGridView1.DoDragDrop(dataGridView1.SelectedRows, DragDropEffects.Move);
}

DoDragDrop method of a control is used to start a drag and drop operation. We call it from MouseDown event of the DataGridView. First parameter is the data that we want to send in drag and drop operation. Here we are sending selected rows of the DataGridView. Second parameter is a DragDropEffects enumeration that provides drag and drop operation effect. Cursor style changes according to this while drag and drop. Possible values are DragDropEffects.All, DragDropEffects.Copy, DragDropEffects.Link, DragDropEffects.Move, DragDropEffects.None and DragDropEffects.Scroll.

  • Write following in DragEnter event of the ListBox
private void listBox1_DragEnter(object sender, DragEventArgs e)
{
     if (e.Data.GetDataPresent(typeof(DataGridViewSelectedRowCollection)))
     {
          e.Effect = DragDropEffects.Move;
     }
}

DragEnter event fired when when an object is dragged into control’s area. DragEventArgs.Data.GetDataPresent() method is used to check if dragged data is a type of DataGridViewSelectedRowCollection. Then drag effect is set using DragEventArgs.Effect property. It is set to DragDropEffects.Move to show dragged operation as move

  • Write following in 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);
     }
}

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 DragEventArgs .Data.GetData() method. Then a foreach loop is used to add rows of this collection to items of the ListBox using ListBox.Items.Add() method. At last that row is removed from the DataGridView using DataGridView.Rows.Remove() method

Conclusion

You can select a row of the DataGridView by clicking on the row header and drag it  from the DataGridView and drop it to the ListBox. Dragged rows are removed from the DatGridView and added to the ListBox. To select more than one row you can press Ctrl key while selecting and to select all the rows click on the top left blank cell.


Filed under: .Net, WinForm

Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images