Reputation: 26635
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?
Upvotes: 16
Views: 20428
Reputation: 4359
I found that this solution worked for me on all DataGrids; even ones that had hidden columns.
// 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);
Upvotes: 1
Reputation: 26635
I find the solution. First of all I have needed column number of the focused cell. I have managed to get it with this code:
DataGridResults.CurrentCell.Column.DisplayIndex;
Then in CopyingRowClipboardContent
event, I must delete all other column values.
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++;
}
}
}
Upvotes: 2
Reputation: 3147
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 );
}
Upvotes: 24
Reputation: 827
You can also use the following code in order control clipboard content.
Clipboard.SetText("some value");
Upvotes: 4