2013-01-07 89 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