2010-01-18 5 views

Répondre

2

La classe DataGridViewRow possède une méthode .Clone qui clone la ligne actuelle qu'elle contient.

Jetez un oeil here pour plus d'infos

+0

Je l'ai vérifié. Mais après avoir copié des données quand je les colle dans une nouvelle rangée d'une grille. Il ne colle pas les données de la ligne précédente alors que lorsque je colle dans une seule zone de texte, cela se produit. Je viens de copier la fonction de code msdn coller après la fonction de DataGridView. – Programmer

7

J'ai trouvé un post qui contient le code à coller des valeurs de presse-papiers dans DataGridView.

j'été googler comment coller à DataGridView en C# du presse-papiers, une info , copié à partir d'Excel, et n'a pas trouver réponse complète. Recueilli un couple de discussions à partir des forums et est venu avec cette réponse, espérons que cela fera la vie de quelqu'un plus facile. Vous ne devez pas comprendre le code juste et copier coller

Voici une version modifiée bits. Au-delà du petit refactoring, j'interdis de coller dans les cellules ReadOnly.

Exemple d'utilisation:

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

Beau code.Pas prêt pour le prime time mais la plupart du temps là-bas. Je l'ai utilisé dans un projet personnel et fonctionne aussi longtemps que vous laissez lentement le 'v' dans ctrl + v d'abord, et la grille doit être dans EditMode de EditOnKeystrokeOrF2 – Mario

0

Copie d'une ligne à l'autre? Pourquoi tout simplement pas aller pour elle comme ça

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

vous pouvez également consulter: http://canlu.blogspot.com/2009/06/copying-datagridviewrow-to-another.html

Hope this helps! :}

0

Voici ma version modifiée de alex2k8 réponse cela fonctionnera pour datagridview lié.

est ici la version non modifiée

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

ici est ma modification arrive que quelqu'un l'espoir se est aidé ici dt est une 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 

REMARQUE: le code est en vb.net utilisez http://converter.telerik.com/ pour le reconvertir en C#

Questions connexes