[Solved] I want to drag some DataGridViewRows from a DataGridView, contained in a TabPage, to another DataGridView also contained in another TabPage. I have set the DataGridView's Event (How could I Drag and Drop DataGridView Rows under each other?), but i don't know how I can "navigate" between TabPages!
Asked
Active
Viewed 415 times
2 Answers
2
Here's a bare bone example how I can drag text from one textbox on a tab to another one on a separate tab:
private void textBox1_MouseDown(object sender, MouseEventArgs e)
{
textBox1.DoDragDrop(textBox1.Text, DragDropEffects.Copy | DragDropEffects.Move);
}
private void tabControl1_DragOver(object sender, DragEventArgs e)
{
Point location = tabControl1.PointToClient(Control.MousePosition);
for (int tab = 0; tab < tabControl1.TabCount; ++tab)
{
if (tabControl1.GetTabRect(tab).Contains(location))
{
tabControl1.SelectedIndex = tab;
break;
}
}
}
private void textBox2_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Text))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
private void textBox2_DragDrop(object sender, DragEventArgs e)
{
textBox2.Text = e.Data.GetData(DataFormats.Text).ToString();
}
NOTE: You must set AllowDrop property to true on the TabControl, and of course on the destination control.
Cheers

Luc Morin
- 5,302
- 20
- 39
0
I have solved the question by myself (and @mrlucmorin) :
internal void dgv_MouseDown(object sender, MouseEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
List<DataGridViewRow> result = new List<DataGridViewRow>();
foreach(DataGridViewRow row in dgv.SelectedRows)
{
result.Add(row);
}
dgv.DoDragDrop(result, DragDropEffects.Copy | DragDropEffects.Move);
}
private void dgv_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void dgv_DragDrop(object sender, DragEventArgs e)
{
try
{
DataGridView dataGridView1 = (DataGridView)sender;
List<DataGridViewRow> rows = new List<DataGridViewRow>();
rows = (List<DataGridViewRow>)e.Data.GetData(rows.GetType());
//some stuff
}
}

Nicola Russo
- 133
- 1
- 2
- 9