DragDrop from one dataGridView to another (C#)

  • Thread starter Thread starter dbuchanan
  • Start date Start date
D

dbuchanan

Hello,

I have a form with two similar DataGridViews. They have identical fields. I
want to drag multiple selected rows from one to the other. I have spent the
evening looking for sample code for this with no success in finding a
complete sample. I have tried for hours to work together incomplete samples
to make one work for me without success. Could someone direct me to a
working sample or provide one for me?

It is not important where in the second dgv that the new rows are placed. It
will start empty and be used like a shopping cart.

Thank you very much,
Douglas
 
Hi Douglas,

The first thing you should do is to set the AllowDrop property of the
drag&drop target control to true. The next step is to handle the MouseMove
event of the drag&drop source and the DragEnter and DragDrop events of the
drag&drop target control.

I will illustrate how to do drag&drop operation between two DataGridView
controls with a sample. It requires that you create a WinForm application
project and add two DataGridView controls on the form. The two DataGridView
are called dataGridView1 and dataGridView2. The former is the drag&drop
source and the latter is the drag&drop target.

The following is the code in the form:

public partial class Form1 : Form
{
private void Form1_Load(object sender, EventArgs e)
{
this.dataGridView2.AllowDrop = true;

this.dataGridView1.MouseMove += new
MouseEventHandler(dataGridView1_MouseMove);
this.dataGridView2.DragEnter += new
DragEventHandler(dataGridView2_DragEnter);
this.dataGridView2.DragDrop += new
DragEventHandler(dataGridView2_DragDrop);

this.dataGridView1.RowCount = 3;
this.dataGridView1.Rows[0].Cells[0].Value = "1";
this.dataGridView1.Rows[0].Cells[1].Value = "11";
this.dataGridView1.Rows[1].Cells[0].Value = "2";
this.dataGridView1.Rows[1].Cells[1].Value = "22";
this.dataGridView1.Rows[2].Cells[0].Value = "3";
this.dataGridView1.Rows[2].Cells[1].Value = "33";
}
void dataGridView2_DragDrop(object sender, DragEventArgs e)
{
DataGridViewRow row = e.Data.GetData(typeof(DataGridViewRow))
as DataGridViewRow;
if (row != null)
{
DataGridViewRow newrow = row.Clone() as DataGridViewRow;
for (int i = 0; i < newrow.Cells.Count; i++)
{
newrow.Cells.Value = row.Cells.Value;
}
this.dataGridView2.Rows.Add(newrow);
}
}

void dataGridView2_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(DataGridViewRow)))
{
e.Effect = DragDropEffects.Copy;
}
}

void dataGridView1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{

this.dataGridView1.DoDragDrop(this.dataGridView1.CurrentRow,
DragDropEffects.All);
}
}
}

Build the project and run the application. Drag a row in the dataGridView1
and drop it onto the dataGridView2 and you should see this row is added
into the dataGridView2.

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Linda,

I had to add the statement:
InitializeComponent();
just after the first curly bracket for the datagrids show.

At the following statement indicated I I got an error

this.dataGridView1.RowCount = 3;
this.dataGridView1.Rows[0].Cells[0].Value = "1";
this.dataGridView1.Rows[0].Cells[1].Value = "11"; <<< Here
this.dataGridView1.Rows[1].Cells[0].Value = "2";
this.dataGridView1.Rows[1].Cells[1].Value = "22";
this.dataGridView1.Rows[2].Cells[0].Value = "3";
this.dataGridView1.Rows[2].Cells[1].Value = "33";

=== start of error ===
System.ArgumentOutOfRangeException was unhandled
Message="Index was out of range. Must be non-negative and less than the
size of the collection.\r\nParameter name: index"
Source="mscorlib"
ParamName="index"
StackTrace:
at System.Collections.ArrayList.get_Item(Int32 index)
at System.Windows.Forms.DataGridViewCellCollection.get_Item(Int32
index)
at DGV_DragDropDemo.Form1..ctor() in
C:\Users\dbuchanan\Documents\Visual Studio
2005\Projects\Projects_CSharp\DGV_DragDropDemo\Form1.cs:line 28
at DGV_DragDropDemo.Program.Main() in
C:\Users\dbuchanan\Documents\Visual Studio
2005\Projects\Projects_CSharp\DGV_DragDropDemo\Program.cs:line 17
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[]
args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
=== end of error ===

I don't yet know what this means...

But in the mean time I tried running the sample by commenting out ever other
line of the populating statements to see the dragdrop run with single column
rows.

My results were disappointing. The cursor indicated that it had a payload by
showing a [+] box. But at the mouse up nothing happened. Don't I need a
mouse up event?

What do I do next?

Douglas
 
Hi Douglas,

Thank you for your reply!

I will send you a sample project to your email box. Please run it on your
machine to see if it works.

If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Linda,

I received your sample and yes it works.

The problem I was having, tryig to use the code you posted was that I didn't
realize that I needed to create the rows in the dataGridViews.

Thank you,

Douglas
 
Linda,

I received your sample and yes it works.

The problem I was having, tryig to use the code you posted was that I didn't
realize that I needed to create the rows in the dataGridViews.

Thank you,

Douglas

could you please post how you created the rows in the dataGridViews, I
am not sure how that should be done

thank you
 
hi lind
ur code s very useful to drag and drop single row. can u tell me ? to drag multiple rows
thnks
 
Hi I am trying to do the drag and drop example how do I get the dragged row to show in the second datagridview
 
Hi Linda

At the following statement indicated I got an error

this.dataGridView1.RowCount = 3; <<<<error
this.dataGridView1.Rows[0].Cells[0].Value = "1";
this.dataGridView1.Rows[0].Cells[1].Value = "11";
this.dataGridView1.Rows[1].Cells[0].Value = "2";
this.dataGridView1.Rows[1].Cells[1].Value = "22";
this.dataGridView1.Rows[2].Cells[0].Value = "3";
this.dataGridView1.Rows[2].Cells[1].Value = "33";

It says "RowCount property cannot be set on a data-bound DataGridView control".
How would I fix this error?
Thanks.

Mags92
 
Back
Top