2013-01-07 62 views
2

我已经复制了一些代码并进行了修改以适合我的应用程序。我会继续调整和清理代码,直到我对它满意为止。但是我遇到了一点错误。我有两个datagridviews,并希望将datagridrows从一个移动到另一个。但是,虽然drag&drop事件全部触发,但dataGridView_Routes_DragDrop()将执行日志命令,因为e.Data.GetData中没有数据。我做错了什么?我错过了什么吗?我试图查看几个指南,但没有具体说明这个问题。在DataGridView之间拖放

我怎样才能让datagrid将拖动的datagridrow传递给其他datagrid?

/* Drag & Drop */ 
    private Rectangle dragBoxFromMouseDown; 
    private int rowIndexFromMouseDown; 
    private void dataGridView_Trips_MouseMove(object sender, MouseEventArgs e) 
    { 
     if ((e.Button & MouseButtons.Left) == MouseButtons.Left) 
     { 
      // If the mouse moves outside the rectangle, start the drag. 
      if (dragBoxFromMouseDown != Rectangle.Empty && !dragBoxFromMouseDown.Contains(e.X, e.Y)) 
      { 
       // Proceed with the drag and drop, passing in the list item.      
       DragDropEffects dropEffect = dataGridView_Trips.DoDragDrop(dataGridView_Trips.Rows[rowIndexFromMouseDown], DragDropEffects.Copy); 
      } 
     } 
    } 

    private void dataGridView_Trips_MouseDown(object sender, MouseEventArgs e) 
    { 
     // Get the index of the item the mouse is below. 
     rowIndexFromMouseDown = dataGridView_Trips.HitTest(e.X, e.Y).RowIndex; 
     if (rowIndexFromMouseDown != -1) 
     { 
      // Remember the point where the mouse down occurred. 
      // The DragSize indicates the size that the mouse can move 
      // before a drag event should be started.     
      Size dragSize = SystemInformation.DragSize; 

      // Create a rectangle using the DragSize, with the mouse position being 
      // at the center of the rectangle. 
      dragBoxFromMouseDown = new Rectangle(new Point(e.X - (dragSize.Width/2), e.Y - (dragSize.Height/2)), dragSize); 
     } 
     else 
      // Reset the rectangle if the mouse is not over an item in the ListBox. 
      dragBoxFromMouseDown = Rectangle.Empty; 
    } 

    private void dataGridView_Routes_DragOver(object sender, DragEventArgs e) 
    { 
     e.Effect = DragDropEffects.Copy; 
    } 

    private void dataGridView_Routes_DragDrop(object sender, DragEventArgs e) 
    { 
     if (e.Data.GetDataPresent(typeof(DataRowView))) 
     { 
      // The mouse locations are relative to the screen, so they must be 
      // converted to client coordinates. 
      Point clientPoint = dataGridView_Routes.PointToClient(new Point(e.X, e.Y)); 

      // If the drag operation was a copy then add the row to the other control. 
      if (e.Effect == DragDropEffects.Copy) 
      { 
       DataGridViewRow rowToMove = e.Data(typeof(DataGridViewRow)) as DataGridViewRow; 
       dataGridView_Routes.Rows.Add(rowToMove); 
      } 
     } 
     else 
     { 
      log("Geen data! #01", "Fout"); 
     } 
    } 
    /* End Drag & Drop */ 
+1

的数据类型不是“DataRowView的”。如果您有源代码,请检查'if(e.Data.GetDataPresent(typeof(DataRowView)))'并查看要删除的数据的类型。例如:它可能是文本数据,在这种情况下,类型是System.String – prthrokz

+0

这是对象包含的内容。 http://daven.nl/c/img/so-datagridviewrow.jpg这是一个datagridviewrow ... – Perfection

+0

我很困惑,重建部分修复了应用程序。 – Perfection

回答

3

我不知道。但是下面的功能已经调整过了,它按预期工作。不太确定之前的代码是如何破解的。

编辑:typeof是用DataViewRow而不是DataGridViewRow编写的。失败。

private void dataGridView_Routes_DragDrop(object sender, DragEventArgs e) 
    { 
     try 
     { 
      if (e.Data.GetDataPresent(typeof(DataGridViewRow))) 
      { 

       // The mouse locations are relative to the screen, so they must be 
       // converted to client coordinates. 
       Point clientPoint = dataGridView_Routes.PointToClient(new Point(e.X, e.Y)); 

       // If the drag operation was a copy then add the row to the other control. 
       if (e.Effect == DragDropEffects.Copy) 
       { 
        DataGridViewRow Row = (DataGridViewRow)e.Data.GetData(typeof(DataGridViewRow)); 
        dataGridView_Routes.Rows.Add(Row.Cells[0].Value, Row.Cells[1].Value, Row.Cells[2].Value); 
       } 
      } 
      else 
      { 
       log("Geen data! #01", "Fout"); 
      } 
     } 
     catch (Exception msg) 
     { 
      log(msg.Message,"Fout"); 
     } 
    } 
+0

回顾这个答案,问题是typeof()类型, 哈哈。这很愚蠢。 – Perfection

3

此解决方案适用于将datagridView绑定到customObjects的人员。它像多种选择一样具有魅力。建议被接受。

假设你想从datagridview1拖动到你试图除去datagridview2

//datagridview1 is bound to this BindingList 
BindingList<myObject> object_bound_list1; 

//datagridview2 is bound to this BindingList 
BindingList<myObject> object_bound_list2; 

List<myObject> selected_Object_list = new List<myObject>(); 
List<int> selected_pos_list = new List<int>(); 

private void dataGridView1_MouseMove(object sender, MouseEventArgs e) 
{ 
    if ((e.Button & MouseButtons.Left) == MouseButtons.Left) 
    { 
     // Proceed with the drag and drop, passing in the list item.     
     DragDropEffects dropEffect = dataGridView1.DoDragDrop(
     selected_Object_list, 
     DragDropEffects.Move); 
    } 
} 

private void dataGridView1_MouseDown(object sender, MouseEventArgs e) 
{ 
    // Get the index of the item the mouse is below. 
    int rowIndexFromMouseDown = dataGridView1.HitTest(e.X, e.Y).RowIndex; 

    //if shift key is not pressed 
    if (Control.ModifierKeys != Keys.Shift && Control.ModifierKeys != Keys.Control) 
    { 
     //if row under the mouse is not selected 
     if (!selected_pos_list.Contains(rowIndexFromMouseDown) && rowIndexFromMouseDown > 0) 
     { 
     //if there only one row selected 
      if (dataGridView1.SelectedRows.Count == 1) 
      { 
       //select the row below the mouse 
       dataGridView.ClearSelection(); 
       dataGridView1.Rows[rowIndexFromMouseDown].Selected = true; 
      } 
     } 
    } 

    //clear the selection lists 
    selected_Object_list.Clear(); 
    selected_pos_list.Clear(); 

    //add the selected objects 
    foreach (DataGridViewRow row in dataGridView1.SelectedRows) 
    { 
     selected_Object_list.Add(object_bound_list1[row.Index]); 
     selected_pos_list.Add(row.Index); 
    } 
} 

private void dataGridView2_DragOver(object sender, DragEventArgs e) 
{ 
    e.Effect = DragDropEffects.Move; 
} 

private void dataGridView2_DragDrop(object sender, DragEventArgs e) 
{ 
    if (e.Effect == DragDropEffects.Move) 
    { 
     foreach (var item in selected_Object_list) 
     { 
      object_bound_list2.Add(item); 
     } 
    } 
} 
+0

我相信“// if shift key is not pressed”下的代码是为了允许自动多重选择而设计的。它似乎没有按预期工作。删除(或将其留下)仍然提供可行的拖放机制。 – JFish222

+0

@ JFish222如果按下shift键,您可能需要选择之前选择的所有行到鼠标下的所有行。如果你不放这个限制,所有先前选择的行将被取消选中。试试吧。 –

+0

爱那些边缘情况! :)好吧,所以[测试1 - 未修改的代码]:我有行1-2突出显示(按住Ctrl单击)。我移动+单击行5.行2-5现在突出显示。 [测试2 - 未修改的代码]:我突出显示了1-2行(按住SHIFT键单击)。我移动+点击行5.行1-5现在突出显示。重复测试删除if语句。行为不变。我在这里有一个脑屁吗?如果你想切换到聊天lmk。谢谢。 – JFish222