2014-01-15 38 views
7

我有2個datagridviews,我想將datagridview1中的單元格複製到datagridview2(單元格一次)。我可以選擇我想要的單元格並將其拖到datagridview2但價值沒有顯示... 我花了大半夜尋找解決方案...可能是一個簡單的答案或我只需要睡覺,但請幫助.... 我有以下代碼將datagridview中的單元格拖放到另一個

 private void dataGridView1_MouseDown(object sender, MouseEventArgs e) 
    { 
     if (e.Button == MouseButtons.Left) 
     { 
      DataGridView.HitTestInfo info = dataGridView1.HitTest(e.X, e.Y); 
      if (info.RowIndex >= 0) 
      { 
       if (info.RowIndex >= 0 && info.ColumnIndex >= 0) 
       { 
        string text = (String) 
          dataGridView1.Rows[info.RowIndex].Cells[info.ColumnIndex].Value; 
        if (text != null) 
         dataGridView1.DoDragDrop(text, DragDropEffects.Copy); 
       } 
      } 
     } 
    } 

    private void dataGridView2_DragDrop(object sender, DragEventArgs e) 
    { 
     string cellvalue=e.Data.GetData(typeof(string)) as string; 
     Point cursorLocation=this.PointToClient(new Point(e.X,e.Y)); 

     System.Windows.Forms.DataGridView.HitTestInfo hittest= dataGridView2.HitTest(cursorLocation.X,cursorLocation.Y); 
     if (hittest.ColumnIndex != -1 
      && hittest.RowIndex != -1) 
      dataGridView2[hittest.ColumnIndex, hittest.RowIndex].Value = cellvalue; 
    } 

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

而designer.cs我有

// dataGridView1 
     // 
     this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; 
     this.dataGridView1.Location = new System.Drawing.Point(12, 12); 
     this.dataGridView1.Name = "dataGridView1"; 
     this.dataGridView1.Size = new System.Drawing.Size(299, 150); 
     this.dataGridView1.TabIndex = 0; 
     this.dataGridView1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.dataGridView1_MouseDown); 
     // 
     // dataGridView2 
     // 
     this.dataGridView2.AllowDrop = true; 
     this.dataGridView2.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; 
     this.dataGridView2.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { 
     this.Column1, 
     this.Column2}); 
     this.dataGridView2.Location = new System.Drawing.Point(353, 141); 
     this.dataGridView2.Name = "dataGridView2"; 
     this.dataGridView2.Size = new System.Drawing.Size(240, 150); 
     this.dataGridView2.TabIndex = 5; 
     this.dataGridView2.DragDrop += new System.Windows.Forms.DragEventHandler(this.dataGridView2_DragDrop); 
     this.dataGridView2.DragEnter += new System.Windows.Forms.DragEventHandler(this.dataGridView2_DragEnter); 
     // 

回答

9

您可以使用以下代碼。我測試了它,它正在將單元格數據從一個datagridview複製到另一個datagridview。

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

    /* Drag & Drop */ 
    private Rectangle dragBoxFromMouseDown; 
    private object valueFromMouseDown; 
    private void dataGridView1_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 = dataGridView1.DoDragDrop(valueFromMouseDown, DragDropEffects.Copy); 
      } 
     } 
    } 

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

     if (hittestInfo.RowIndex != -1 && hittestInfo.ColumnIndex != -1) 
     { 
      valueFromMouseDown = dataGridView1.Rows[hittestInfo.RowIndex].Cells[hittestInfo.ColumnIndex].Value; 
      if (valueFromMouseDown != null) 
      { 
       // 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 dataGridView2_DragOver(object sender, DragEventArgs e) 
    { 
     e.Effect = DragDropEffects.Copy; 
    } 

    private void dataGridView2_DragDrop(object sender, DragEventArgs e) 
    { 
     // The mouse locations are relative to the screen, so they must be 
     // converted to client coordinates. 
     Point clientPoint = dataGridView2.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) 
     { 
      string cellvalue = e.Data.GetData(typeof(string)) as string; 
      var hittest = dataGridView2.HitTest(clientPoint.X, clientPoint.Y); 
      if (hittest.ColumnIndex != -1 
       && hittest.RowIndex != -1) 
       dataGridView2[hittest.ColumnIndex, hittest.RowIndex].Value = cellvalue; 

     } 
    } 
1

你可能需要改變你的代碼::

private void dataGridView1_MouseDown(object sender, MouseEventArgs e) 
{ 
    if (e.Button == MouseButtons.Left) 
    { 
     DataGridView.HitTestInfo info = dataGridView1.HitTest(e.X, e.Y); 
     if (info.RowIndex >= 0) 
     { 
      if (info.RowIndex >= 0 && info.ColumnIndex >= 0) 
      { 
       string text = (String) 
         dataGridView1.Rows[info.RowIndex].Cells[info.ColumnIndex].Value; 
       if (text != null){ 
        //Need to put braces here CHANGE 
        dataGridView1.DoDragDrop(text, DragDropEffects.Copy); 
       } 
      } 
     } 
    } 
} 

private void dataGridView2_DragDrop(object sender, DragEventArgs e) 
{ 
    string cellvalue=e.Data.GetData(typeof(string)) as string; 
    Point cursorLocation=this.PointToClient(new Point(e.X,e.Y)); 

    System.Windows.Forms.DataGridView.HitTestInfo hittest= dataGridView2.HitTest(cursorLocation.X,cursorLocation.Y); 
    if (hittest.ColumnIndex != -1 
     && hittest.RowIndex != -1){ //CHANGE 
     dataGridView2[hittest.ColumnIndex, hittest.RowIndex].Value = cellvalue; 
    } 
} 

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

這部分代碼的dataGridView1.DoDragDrop(text, DragDropEffects.Copy);應該是括號內爲你檢查之前,爲了它的條件,如(texts!=null)

+0

謝謝你的建議,但我仍然有同樣的問題... –

+0

@ user3061846我再次編輯代碼,請再次嘗試。 –

+0

仍然一樣:( –

1

的錯誤是在這一行:

Point cursorLocation = this.PointToClient(new Point(e.X,e.Y)); 

因爲this.不指DataGridView,而是到Form

應該改爲:

Point cursorLocation = dataGridView2.PointToClient(new Point(e.X,e.Y)); 
相關問題