Search code examples
c#wpfwpfdatagrid

How to copy DataGrid cell value to clipboard


I have a DataGrid. But I want to get focused cell value in CopyingRowClipboardContent event. But e.ClipboardRowContent returns me all selected cells values because of the SelectionUnit. And i must not change selection unit of datagrid. For solving the problem i need to get focused cell column number. Then I will remove all column values from clipboarcContent. How can i get focused cell in CopyingRowClipboardContent event?


Solution

  • Improved version of Farhad's answer

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