2010-01-18 105 views

回答

2

DataGridViewRow类有一个.Clone方法,它将克隆它所保存的当前行。

有更多的信息来看看here

+0

我检查了它。但在复制数据后,我将其粘贴到gridview的新行。它不会粘贴上一行的数据,而当我在单个文本框中粘贴时发生。 我只是在DataGridView的函数后面复制了粘贴msdn代码的函数。 – Programmer 2010-01-19 12:41:16

7

我已经找到了post包含代码从剪贴板中值粘贴到DataGridView的。

我google搜索如何粘贴到 的DataGridView在C#中从剪贴板中, 信息,从Excel中复制,并没有发现 全面的答案。收集来自论坛的 线程,并想出了 这个答案,希望这会让 有些人的生活更轻松。你不必 理解代码只是复制和粘贴

下面是有点修改后的版本。除了小的重构之外,我禁止将其粘贴到ReadOnly单元格中。

用例:

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

代码:

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

不错的代码。没有准备好黄金时段,但大部分的路上。我在一个个人项目中使用它,只要你先慢慢地按下ctrl + v中的'v',并且网格必须在EditOnKeystrokeOrF2的EditMode中 – Mario 2012-12-04 16:11:40

0

复制一行到另一个?为什么根本不买账这样

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; 
} 

你还可以参考:http://canlu.blogspot.com/2009/06/copying-datagridviewrow-to-another.html

希望这有助于! :}

0

下面是我alex2k8回答修改后的版本,这将用于绑定datagridview的工作。

这里是未修改的版本

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

这里是我的修改发生希望有人来到这里帮助dt为一个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 

注:该代码是在vb.net使用http://converter.telerik.com/将其转换回c#