I am having trouble copying cells from a WPF datagrid. I have a few other text editor windows (like Avalonedit) in the same app from where I can copy to Clipboard but not from the datagrid. Whenever, I try to copy even one cell I get the message "Open Clipboard Failed (Exception from HRESULT: 0x800401D0).
So I can copy paste everything else except from the datagrid. I tried the OpenClipboard and CloseClipboard approach but that didn't seem to work for me either.
I have looked at the approach mentioned here. (http://blog.somewhatabstract.com/2012/06/27/when-the-clipboard-says-no/) but I am unable to find out how to override the method OnExecutingCopy unless I derive from a DataGrid and do something.
@ssarangi
Hi ssarangi...I don't know if you ever solved this clipboard issue, but I encountered your question while trying to find a solution to a clipboard-contention problem some of my users are having intermittently. What I'm doing is a two-pronged approach:
I answered another, similar question in which I included the code snippet, so if you're still looking for some assistance you can check it out and see if it helps: OpenClipboard Failed when copy pasting data from wpf DataGrid
In essence I used two DllImport's for GetOpenClipboardWindow() and GetWindowThreadProcessId(), and then I get all the active Process objects via Process.GetProcesses() and iterate through them looking for a match on either the window handle from GetOpenClipboardWindow() or a match on Process.Id (the PID) I obtained via GetWindowThreadProcessId(). This isn't a solution, but it might help identify what is locking you out of the clipboard.
The other thing I'm doing which again might help is switching to the System.Windows.Forms Clipboard class with the built-in retry loop for SetDataObject().
I know this is an older post, but this solution is posted for completeness and is missing the use of a suited DataGrid event method signature associated with the DataGridRowClipboardEventArgs.
Clipboard.SetText can be flaky, not grabbing/setting the clipboard all the time.
Set "FullRow" at the SelectionUnit mode for dataGrid called myDataGrid
<DataGrid x:Name="myDataGrid" SelectionUnit="FullRow"></DataGrid>
We have a method myDataGrid_CopyingRowClipboardContent that gets called for each row in the dataGrid to copy its contents to the clipboard. For example,for a datagrid with 7 rows this is called 7 times.
public int clipboardcalledcnt { get; set; } //CopyingRowClipboardContent invoked count
private void myDataGrid_CopyingRowClipboardContent(object sender, DataGridRowClipboardEventArgs e)
{
PathInfo cellpath = new PathInfo(); //a custom class to hold path info
string path = string.Empty;
DataGrid dgdataPaths = (DataGrid)sender;
int rowcnt = dgdataPaths.SelectedItems.Count;
cellpath = (PathInfo)e.Item;
path = "Row #"+ clipboardcalledcnt +" Len="+ cellpath.Length.ToString() + ", path=" + cellpath.Path;
e.ClipboardRowContent.Clear();
if (clipboardcalledcnt == 0) //add header to clipboard paste
e.ClipboardRowContent.Add(new DataGridClipboardCellContent("", null, "--- Clipboard Paste ---\t\t\n")); // \t cell divider, repeat (number of cells - 1)
clipboardcalledcnt++;
e.ClipboardRowContent.Add(new DataGridClipboardCellContent(path, null, path));
if (clipboardcalledcnt == rowcnt)
clipboardcalledcnt = 0;
}
User contributions licensed under CC BY-SA 3.0