2012-09-20 21 views
6

Mam DataGrid. Ale chcę uzyskać skoncentrowaną wartość komórki w wydarzeniu CopyingRowClipboardContent. Ale e.ClipboardRowContent zwraca mi wszystkie wybrane wartości komórek ze względu na SelectionUnit. I nie mogę zmienić jednostki wyboru datagridu. Aby rozwiązać problem, muszę uzyskać skoncentrowany numer kolumny komórki. Następnie usunę wszystkie wartości kolumn z clipboarcContent. Jak mogę uzyskać skoncentrowaną komórkę w wydarzeniu CopyingRowClipboardContent?Jak skopiować wartość komórki DataGrid do schowka

Odpowiedz

11

Ulepszona wersja odpowiedzi farhad za

private void DataGrid_CopyingRowClipboardContent(object sender, DataGridRowClipboardEventArgs e) 
{ 
    var currentCell = e.ClipboardRowContent[ dataGrid.CurrentCell.Column.DisplayIndex]; 
    e.ClipboardRowContent.Clear(); 
    e.ClipboardRowContent.Add(currentCell); 
} 
1

Znajduję rozwiązanie. Przede wszystkim potrzebowałem numeru kolumny skupionej komórki. Udało mi się dostać z tym kodem:

DataGridResults.CurrentCell.Column.DisplayIndex; 

Następnie w CopyingRowClipboardContent razie muszę usunąć wszystkie inne wartości kolumn.

private void DataGridResults_CopyingRowClipboardContent(object sender, DataGridRowClipboardEventArgs e) 
{ 
    int y = 0; 

    for (int i = 0; i < e.EndColumnDisplayIndex; i++) 
    { 
     if (i != DataGridResults.CurrentCell.Column.DisplayIndex) 
     { 
      e.ClipboardRowContent.RemoveAt(i - y); 
      y++; 
     } 
    } 
} 
4

Możesz również użyć następującego kodu w schowku sterującym zamówieniem.

Clipboard.SetText("some value"); 
+0

Dzięki, jest również przydatny. –

1

Okazało się, że to rozwiązanie pracował dla mnie na wszystkich datagrids; nawet te, które miały ukryte kolumny.

// Clipboard Row content only includes entries for visible cells 
// Figure out the actual column we are looking for (taking into account hidden columns) 
int columnIndex = dataGrid.CurrentCell.Column.DisplayIndex; 
var column = dataGrid.Columns[columnIndex]; 

// Find the associated column we're interested in from the clipboard row content 
var cellContent = clipboardRowContent.Where(item => item.Column == column).First(); 
clipboardRowContent.Clear(); 
clipboardRowContent.Add(cellContent); 
Powiązane problemy