Windows Develop Bookmark and Share   
 index > Windows Forms General > Drag drop datagrid rows in different forms
 

Drag drop datagrid rows in different forms

Hi all!!!

I would like to code some feature in my application. I know how to drag and drop multiple rows from a one datagridview to another, but both datagridviews are in the same window form.

As you can see, I do something like tthat when the user just dragdrop
private void PointDataGridView_DragDrop (object sender, DragEventArgs e) {

    for (int i = this._searchedDataGridView.SelectedRows.Count; i > 0; i--) {
        ...
    }
}
Now, I would like to drag and drop some data from a datagridview which is form1 to another datagridview which is in form2.

Does someone can help me?

Thank you in advance!
Gustavo
  • Moved byHarry ZhuMSFTThursday, October 01, 2009 3:35 AMrelating to drag rows of datagridview (From:Visual C# General)
  •  
gborgesbr  Tuesday, September 29, 2009 2:23 PM

Hi,

 

First, you need to set AllowDrop property of the two datagridview to true.

 

Assume form1 has a datagridview control named dataGridView1, form2 has a DataGridView control named dataGridView2.

We need to drag rows of datagridView2 to datagridview1.

 

In form2, we need to handle some event of datagridview2. I would like to use mousemove event to drag. If you click one cell use MouseButton.Left, this cell will get selected, and this will clear the previous selection. So you will only get one cell selected. To keep multiple rows selected when you drag, it will be easy using MouseButton.Right. Then keep the selected rows value in a collection. You need to expose the value collection to form1 in order to add the value to form1.datagridview1.

The following is an example. Not perfect but can give you a hint.

Form1:

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

            dataGridView1.Rows.Add("11", "11");

            dataGridView1.Rows.Add("11", "11");

 

            dataGridView1.AllowDrop = true;

            dataGridView1.DragDrop += new DragEventHandler(dataGridView1_DragDrop);

            dataGridView1.DragOver += new DragEventHandler(dataGridView1_DragOver);

        }

 

        private void dataGridView1_DragOver(object sender, DragEventArgs e)

        {

            e.Effect = DragDropEffects.Move;

        }

 

        Form2 f2;

        private void button1_Click(object sender, EventArgs e)

        {

            f2 = new Form2(this);

            f2.Show();

        }

 

        private void dataGridView1_DragDrop(object sender, DragEventArgs e)

        {

           

            if (e.Effect == DragDropEffects.Move)

            {

                // Only add rows.

                //If you need insert the row where mouse down, you need use dataGridView1.HitTest method to detemine the index.

                //please refer to the FAQ.

                for (int i = f2.arrmove.Count - 1; i >= 0; i--)

                {

                    ArrayList rowvalue = (ArrayList)f2.arrmove[i];

                    dataGridView1.Rows.Add(rowvalue[0], rowvalue[1]);

                }

                f2.removeRow();

            }

        }

 

    }

 


Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Tuesday, October 06, 2009 1:18 PM

Form2:

    public partial class Form2 : Form

    {

        Form1 form1;

 

        // Pass form1

        public Form2(Form1 f)

        {

            InitializeComponent();

            form1 = f;

        }

 

        private void Form2_Load(object sender, EventArgs e)

        {

            dataGridView2.AllowDrop = true;

            dataGridView2.Rows.Add("1", "aa");

            dataGridView2.Rows.Add("2", "aa");

            dataGridView2.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;

      

            dataGridView2.DragOver += new DragEventHandler(dataGridView1_DragOver);

            dataGridView2.MouseMove += new MouseEventHandler(dataGridView1_MouseMove);

        }

 

        public ArrayList arrmove;

 

        private void dataGridView1_MouseMove(object sender, MouseEventArgs e)

        {

            if ((e.Button & MouseButtons.Right) == MouseButtons.Right)

            {

                if (dataGridView2.SelectedRows.Count > 0)

                {

                    arrmove = new ArrayList();

 

                    foreach (DataGridViewRow dr in dataGridView2.SelectedRows)

                    {

                        if (dr.Index != dataGridView2.NewRowIndex)

                        {

                            ArrayList rowValue = new ArrayList();

 

                            foreach (DataGridViewCell cell in dr.Cells)

                            {

                                rowValue.Add(cell.Value);

                            }

 

                            arrmove.Add(rowValue);

                        }

                    }

 

                    // avoid only one selected row is the new row

                    if (arrmove.Count != 0)

                    {

                        DragDropEffects dropEffect = dataGridView2.DoDragDrop(arrmove, DragDropEffects.Move);

                        form1.Activate();

                    }

                }

            }

        }

 

        private void dataGridView1_DragOver(object sender, DragEventArgs e)

        {

            e.Effect = DragDropEffects.Move;

        }

 

        /// <summary>

        /// Remove the selected rows

        /// </summary>

        public void removeRow()

        {

            foreach (DataGridViewRow dr in dataGridView2.SelectedRows)

            {

                if (dr.Index != dataGridView2.NewRowIndex)

                {

                    dataGridView2.Rows.Remove(dr);

                }

            }

        }

 

    }

 

FAQ: 22.How do I perform drag and drop reorder of rows?
http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/a44622c0-74e1-463b-97b9-27b87513747e#faq22

Best regards,

Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Tuesday, October 06, 2009 1:18 PM
This not an exact answer to your question .... but perhaps it will be helpful in some way.
This is a drag drop operation of files for example from a file explorer .............. to a datagridview.

		private void folder_file_dgv_DragDrop(object sender, DragEventArgs e)
		{
			if (e.Data.GetDataPresent(DataFormats.FileDrop))
			{
				var o = e.Data.GetData(DataFormats.FileDrop);
				var sequence = o as string[];
				if (sequence != null)
				{
					AddFileList(sequence);
				}
			}

software_writer_nyc  Tuesday, September 29, 2009 10:32 PM

Hi,

 

First, you need to set AllowDrop property of the two datagridview to true.

 

Assume form1 has a datagridview control named dataGridView1, form2 has a DataGridView control named dataGridView2.

We need to drag rows of datagridView2 to datagridview1.

 

In form2, we need to handle some event of datagridview2. I would like to use mousemove event to drag. If you click one cell use MouseButton.Left, this cell will get selected, and this will clear the previous selection. So you will only get one cell selected. To keep multiple rows selected when you drag, it will be easy using MouseButton.Right. Then keep the selected rows value in a collection. You need to expose the value collection to form1 in order to add the value to form1.datagridview1.

The following is an example. Not perfect but can give you a hint.

Form1:

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

            dataGridView1.Rows.Add("11", "11");

            dataGridView1.Rows.Add("11", "11");

 

            dataGridView1.AllowDrop = true;

            dataGridView1.DragDrop += new DragEventHandler(dataGridView1_DragDrop);

            dataGridView1.DragOver += new DragEventHandler(dataGridView1_DragOver);

        }

 

        private void dataGridView1_DragOver(object sender, DragEventArgs e)

        {

            e.Effect = DragDropEffects.Move;

        }

 

        Form2 f2;

        private void button1_Click(object sender, EventArgs e)

        {

            f2 = new Form2(this);

            f2.Show();

        }

 

        private void dataGridView1_DragDrop(object sender, DragEventArgs e)

        {

           

            if (e.Effect == DragDropEffects.Move)

            {

                // Only add rows.

                //If you need insert the row where mouse down, you need use dataGridView1.HitTest method to detemine the index.

                //please refer to the FAQ.

                for (int i = f2.arrmove.Count - 1; i >= 0; i--)

                {

                    ArrayList rowvalue = (ArrayList)f2.arrmove[i];

                    dataGridView1.Rows.Add(rowvalue[0], rowvalue[1]);

                }

                f2.removeRow();

            }

        }

 

    }

 


Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Tuesday, October 06, 2009 1:18 PM

Form2:

    public partial class Form2 : Form

    {

        Form1 form1;

 

        // Pass form1

        public Form2(Form1 f)

        {

            InitializeComponent();

            form1 = f;

        }

 

        private void Form2_Load(object sender, EventArgs e)

        {

            dataGridView2.AllowDrop = true;

            dataGridView2.Rows.Add("1", "aa");

            dataGridView2.Rows.Add("2", "aa");

            dataGridView2.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;

      

            dataGridView2.DragOver += new DragEventHandler(dataGridView1_DragOver);

            dataGridView2.MouseMove += new MouseEventHandler(dataGridView1_MouseMove);

        }

 

        public ArrayList arrmove;

 

        private void dataGridView1_MouseMove(object sender, MouseEventArgs e)

        {

            if ((e.Button & MouseButtons.Right) == MouseButtons.Right)

            {

                if (dataGridView2.SelectedRows.Count > 0)

                {

                    arrmove = new ArrayList();

 

                    foreach (DataGridViewRow dr in dataGridView2.SelectedRows)

                    {

                        if (dr.Index != dataGridView2.NewRowIndex)

                        {

                            ArrayList rowValue = new ArrayList();

 

                            foreach (DataGridViewCell cell in dr.Cells)

                            {

                                rowValue.Add(cell.Value);

                            }

 

                            arrmove.Add(rowValue);

                        }

                    }

 

                    // avoid only one selected row is the new row

                    if (arrmove.Count != 0)

                    {

                        DragDropEffects dropEffect = dataGridView2.DoDragDrop(arrmove, DragDropEffects.Move);

                        form1.Activate();

                    }

                }

            }

        }

 

        private void dataGridView1_DragOver(object sender, DragEventArgs e)

        {

            e.Effect = DragDropEffects.Move;

        }

 

        /// <summary>

        /// Remove the selected rows

        /// </summary>

        public void removeRow()

        {

            foreach (DataGridViewRow dr in dataGridView2.SelectedRows)

            {

                if (dr.Index != dataGridView2.NewRowIndex)

                {

                    dataGridView2.Rows.Remove(dr);

                }

            }

        }

 

    }

 

FAQ: 22.How do I perform drag and drop reorder of rows?
http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/a44622c0-74e1-463b-97b9-27b87513747e#faq22

Best regards,

Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Tuesday, October 06, 2009 1:18 PM

You can use google to search for other answers

Custom Search

More Threads

• differentiate between right and left-click??
• C# Windows Application Detect Run Mode
• Region IsVisible problem
• GDI+ LineCap width
• how to work with "System.Drawing.Imaging.EncoderParameters"?
• How to get the column index in the EditingControlShowing Event ?
• How to save RichTextBox formatting?
• printpreview
• Download options
• trapping focus to a control