2010-01-18 23 views
9

Tôi đang làm việc trên một ứng dụng C# windows và muốn sao chép một hàng trong một DataGridView và dán nó vào một hàng mới. Làm thế nào tôi có thể đạt được điều này? Tôi đang sử dụng .net framework 3.5.Chức năng sao chép/dán hàng trong DataGridview (ứng dụng cửa sổ)

Bạn có thể vui lòng cung cấp cho tôi một số ý tưởng hoặc một số mã chỉ ra cách tôi có thể đạt được điều này không?

Trả lời

2

DataGridView Lớp học có phương thức .Clone sẽ sao chép hàng hiện tại mà nó giữ.

Có một cái nhìn here để biết thêm

+0

Tôi đã kiểm tra. Nhưng sau khi sao chép dữ liệu khi tôi dán nó vào hàng mới của một GridView. Nó không dán dữ liệu của hàng trước đó trong khi khi tôi dán vào một hộp văn bản duy nhất .. nó xảy ra. Tôi chỉ sao chép dán chức năng mã msdn sau hàm của DataGridView. – Programmer

7

Tôi đã tìm thấy một post có chứa mã để dán giá trị từ clipboard vào DataGridView.

Tôi đã googling làm thế nào để dán vào DataGridView trong C# từ clipboard, một thông tin , sao chép từ Excel, và không tìm câu trả lời đầy đủ. Đã thu thập được một cặp số chủ đề từ các diễn đàn và đã đưa ra câu trả lời này , hy vọng nó sẽ làm cho cuộc sống của người nào đó dễ dàng hơn. Bạn không cần phải hiểu mã chỉ cần sao chép và dán

Dưới đây là phiên bản được sửa đổi chút. Ngoài việc tái cấu trúc nhỏ, tôi cấm dán vào các ô ReadOnly.

Cách sử dụng Ví dụ:

private void dataGridView1_KeyUp(object sender, KeyEventArgs e) 
{ 
    ClipboardUtils.OnDataGridViewPaste(sender, e); 
} 

Code:

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Windows.Forms; 

namespace Commons 
{ 
    public class ClipboardUtils 
    { 
     public static void OnDataGridViewPaste(object grid, KeyEventArgs e) 
     { 
      if ((e.Shift && e.KeyCode == Keys.Insert) || (e.Control && e.KeyCode == Keys.V)) 
      { 
       PasteTSV((DataGridView)grid); 
      } 
     } 

     public static void PasteTSV(DataGridView grid) 
     { 
      char[] rowSplitter = { '\r', '\n' }; 
      char[] columnSplitter = { '\t' }; 

      // Get the text from clipboard 
      IDataObject dataInClipboard = Clipboard.GetDataObject(); 
      string stringInClipboard = (string)dataInClipboard.GetData(DataFormats.Text); 

      // Split it into lines 
      string[] rowsInClipboard = stringInClipboard.Split(rowSplitter, StringSplitOptions.RemoveEmptyEntries); 

      // Get the row and column of selected cell in grid 
      int r = grid.SelectedCells[0].RowIndex; 
      int c = grid.SelectedCells[0].ColumnIndex; 

      // Add rows into grid to fit clipboard lines 
      if (grid.Rows.Count < (r + rowsInClipboard.Length)) 
      { 
       grid.Rows.Add(r + rowsInClipboard.Length - grid.Rows.Count); 
      } 

      // Loop through the lines, split them into cells and place the values in the corresponding cell. 
      for (int iRow = 0; iRow < rowsInClipboard.Length; iRow++) 
      { 
       // Split row into cell values 
       string[] valuesInRow = rowsInClipboard[iRow].Split(columnSplitter); 

       // Cycle through cell values 
       for (int iCol = 0; iCol < valuesInRow.Length; iCol++) 
       { 

        // Assign cell value, only if it within columns of the grid 
        if (grid.ColumnCount - 1 >= c + iCol) 
        { 
         DataGridViewCell cell = grid.Rows[r + iRow].Cells[c + iCol]; 

         if (!cell.ReadOnly) 
         { 
          cell.Value = valuesInRow[iCol]; 
         } 
        } 
       } 
      } 
     } 
    } 
} 
+0

Mã đẹp.Không sẵn sàng cho thời gian chính nhưng hầu hết các con đường ở đó. Tôi đã sử dụng nó trong một dự án cá nhân và hoạt động miễn là bạn từ từ bỏ 'v' trong ctrl + v trước tiên, và lưới phải ở trong EditMode của EditOnKeystrokeOrF2 – Mario

0

Sao chép một hàng khác? Tại sao đơn giản là không đi cho nó như thế này

public DataGridViewRow CloneWithValues(DataGridViewRow row) 
{ 
    DataGridViewRow clonedRow = (DataGridViewRow)row.Clone(); 
    for (Int32 index = 0; index < row.Cells.Count; index++) 
    { 
     clonedRow.Cells[index].Value = row.Cells[index].Value; 
    } 
    return clonedRow; 
} 

Ngoài ra bạn có thể tham khảo: http://canlu.blogspot.com/2009/06/copying-datagridviewrow-to-another.html

Hope this helps! :}

0

Dưới đây là phiên bản sửa đổi của tôi về alex2k8 câu trả lời này sẽ làm việc cho datagridview ràng buộc.

đây là phiên bản chưa sửa đổi

' Add rows into grid to fit clipboard lines 
     if (grid.Rows.Count < (r + rowsInClipboard.Length)) 
     { 
      grid.Rows.Add(r + rowsInClipboard.Length - grid.Rows.Count); 
     } 

ở đây là sửa đổi của tôi xảy ra hy vọng một ai đó đã giúp đây dt là một DataTable

' Add rows into grid to fit clipboard lines 
    If grid.Rows.Count < (r + rowsInClipboard.Length) Then 
     Dim workRow As DataRow 
     Dim i As Integer 

     For i = 0 To (r + rowsInClipboard.Length - grid.Rows.Count) 
      workRow = dt.NewRow() 
      workRow(0) = "" 

      dt.Rows.Add(workRow) 
     Next 
    End If 

Chú ý: mã này là trong vb.net sử dụng http://converter.telerik.com/ để chuyển đổi lại thành C#

Các vấn đề liên quan