2010-11-02 13 views
5

Datagridview'in birden fazla hücreye ait metni kopyalayıp yapıştırmasını sağladığını gördüm, bunu etkinleştirmek için basit bir ayar var mı yoksa anahtarı kullanmak zorunda mıyım? Bu işlevselliği dahil etmek için işleyici ve panoya veri deposu. Bir kullanıcı, bir satır içinde 3 hücreyi kopyalamak ve bu metinleri farklı bir sıraya yapıştırmak isteyebilir.DataGridView içinde birden çok hücreyi kopyalayıp yapıştırın

cevap

5

Tamam, bir fikir geldi ama o hücreleri yapıştırarak test edilmemiştir DataGridView

if (e.Control && e.KeyCode == Keys.C) 
      { 
       DataObject d = AccountGrid.GetClipboardContent(); 
       Clipboard.SetDataObject(d); 
       e.Handled = true; 
      } 
      else if (e.Control && e.KeyCode == Keys.V) 
      { 
       string s = Clipboard.GetText(); 
       string[] lines = s.Split('\n'); 
       int row = AccountGrid.CurrentCell.RowIndex; 
       int col = AccountGrid.CurrentCell.ColumnIndex; 
       string[] cells = lines[0].Split('\t'); 
       int cellsSelected = cells.Length; 
       for (int i = 0; i < cellsSelected; i++) 
       { 
        AccountGrid[col, row].Value = cells[i]; 
        col++; 
       } 
      } 
1
 string s = Clipboard.GetText(); 
     string[] lines = s.Split('\n'); 
     int row = dataGridView1.CurrentCell.RowIndex; 
     int col = dataGridView1.CurrentCell.ColumnIndex; 
     foreach (string line in lines) 
     { 
      string[] cells = line.Split('\t'); 
      int cellsSelected = cells.Length; 
      if (row < dataGridView1.Rows.Count) 
      { 
       for (int i = 0; i < cellsSelected; i++) 
       { 
        if (col + i < dataGridView1.Columns.Count) 
         dataGridView1[col + i, row].Value = cells[i]; 
        else 
         break; 
       } 
       row++; 
      } 
      else 
      { 
       break; 
      } 
     } 
0
if (e.Control && e.KeyCode == Keys.V) 
{ 

string CopiedContent = Clipboard.GetText(); 
string[] Lines = CopiedContent.Split('\n'); 
int StartingRow = dataGridView1.CurrentCell.RowIndex; 
int StartingColumn = dataGridView1.CurrentCell.ColumnIndex; 
foreach (var line in Lines) 
{ 
    if (StartingRow <= (dataGridView1.Rows.Count - 1)) 
    { 
     string[] cells = line.Split('\t'); 
     int ColumnIndex = StartingColumn; 
     for (int i = 0; i < cells.Length && ColumnIndex <= (dataGridView1.Columns.Count - 1); i++) 
     { 
      dataGridView1[ColumnIndex++, StartingRow].Value = cells[i]; 
     } 
     StartingRow++; 
    } 
}} 
KeyDown olayı birden rows.This edilir genelinde