解决了,我想把一些DataGridViewRows从DataGridView (包含在TabPage中)拖到另一个DataGridView (也包含在另一个TabPage中)。我已经设置了DataGridView的事件(How could I Drag and Drop DataGridView Rows under each other?),但是我不知道如何在TabPages之间“导航”!
发布于 2014-02-16 20:28:54
下面是一个简单的示例,可以将文本从标签上的一个文本框拖到另一个单独的选项卡上:
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();
}注意:您必须在AllowDrop上将TabControl属性设置为true,当然在目标控件上也是如此。
干杯
发布于 2014-02-17 15:02:41
我自己解决了这个问题( @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
}
}https://stackoverflow.com/questions/21815480
复制相似问题