/// <summary> /// Copy just the selected issue's word to the clipboard, not the whole row /// </summary> /// <param name="sender">The sender of the event</param> /// <param name="e">The event arguments</param> private void dgIssues_CopyingRowClipboardContent(object sender, DataGridRowClipboardEventArgs e) { var currentCell = e.ClipboardRowContent[4]; e.ClipboardRowContent.Clear(); e.ClipboardRowContent.Add(currentCell); }
protected virtual void OnExecutedCopy(ExecutedRoutedEventArgs args) { if (ClipboardCopyMode == DataGridClipboardCopyMode.None) { throw new NotSupportedException(SR.Get(SRID.ClipboardCopyMode_Disabled)); } args.Handled = true; // Supported default formats: Html, Text, UnicodeText and CSV Collection<string> formats = new Collection<string>(new string[] { DataFormats.Html, DataFormats.Text, DataFormats.UnicodeText, DataFormats.CommaSeparatedValue }); Dictionary<string, StringBuilder> dataGridStringBuilders = new Dictionary<string, StringBuilder>(formats.Count); foreach (string format in formats) { dataGridStringBuilders[format] = new StringBuilder(); } int minRowIndex; int maxRowIndex; int minColumnDisplayIndex; int maxColumnDisplayIndex; // Get the bounding box of the selected cells if (_selectedCells.GetSelectionRange(out minColumnDisplayIndex, out maxColumnDisplayIndex, out minRowIndex, out maxRowIndex)) { // Add column headers if enabled if (ClipboardCopyMode == DataGridClipboardCopyMode.IncludeHeader) { DataGridRowClipboardEventArgs preparingRowClipboardContentEventArgs = new DataGridRowClipboardEventArgs(null, minColumnDisplayIndex, maxColumnDisplayIndex, true /*IsColumnHeadersRow*/); OnCopyingRowClipboardContent(preparingRowClipboardContentEventArgs); foreach (string format in formats) { dataGridStringBuilders[format].Append(preparingRowClipboardContentEventArgs.FormatClipboardCellValues(format)); } } // Add each selected row for (int i = minRowIndex; i <= maxRowIndex; i++) { object row = Items[i]; // Row has a selecion if (_selectedCells.Intersects(i)) { DataGridRowClipboardEventArgs preparingRowClipboardContentEventArgs = new DataGridRowClipboardEventArgs(row, minColumnDisplayIndex, maxColumnDisplayIndex, false /*IsColumnHeadersRow*/, i); OnCopyingRowClipboardContent(preparingRowClipboardContentEventArgs); foreach (string format in formats) { dataGridStringBuilders[format].Append(preparingRowClipboardContentEventArgs.FormatClipboardCellValues(format)); } } } } DataGridClipboardHelper.GetClipboardContentForHtml(dataGridStringBuilders[DataFormats.Html]); DataObject dataObject; bool hasPerms = SecurityHelper.CallerHasAllClipboardPermission() && SecurityHelper.CallerHasSerializationPermission(); // Copy unconditionally in full trust. // Only copy in partial trust if user initiated. if (hasPerms || args.UserInitiated ) { (new UIPermission(UIPermissionClipboard.AllClipboard)).Assert(); try { dataObject = new DataObject(); } finally { UIPermission.RevertAssert(); } foreach (string format in formats) { dataObject.CriticalSetData(format, dataGridStringBuilders[format].ToString(), false /*autoConvert*/); } // This assert is there for an OLE Callback to register CSV type for the clipboard (new SecurityPermission(SecurityPermissionFlag.SerializationFormatter | SecurityPermissionFlag.UnmanagedCode)).Assert(); try { Clipboard.CriticalSetDataObject(dataObject, true /* Copy */); } finally { SecurityPermission.RevertAll(); } } }
/// <summary> /// This method is called to prepare the clipboard content for each selected row. /// If ClipboardCopyMode is set to ClipboardCopyMode, then it is also called to prepare the column headers /// </summary> /// <param name="args">Contains the necessary information for generating the row clipboard content.</param> protected virtual void OnCopyingRowClipboardContent(DataGridRowClipboardEventArgs args) { if (args.IsColumnHeadersRow) { for (int i = args.StartColumnDisplayIndex; i <= args.EndColumnDisplayIndex; i++) { DataGridColumn column = ColumnFromDisplayIndex(i); if (!column.IsVisible) { continue; } args.ClipboardRowContent.Add(new DataGridClipboardCellContent(args.Item, column, column.Header)); } } else { int rowIndex = args.RowIndexHint; if (rowIndex < 0) { rowIndex = Items.IndexOf(args.Item); } // If row has selection if (_selectedCells.Intersects(rowIndex)) { for (int i = args.StartColumnDisplayIndex; i <= args.EndColumnDisplayIndex; i++) { DataGridColumn column = ColumnFromDisplayIndex(i); if (!column.IsVisible) { continue; } object cellValue = null; // Get cell value only if the cell is selected - otherwise leave it null if (_selectedCells.Contains(rowIndex, i)) { cellValue = column.OnCopyingCellClipboardContent(args.Item); } args.ClipboardRowContent.Add(new DataGridClipboardCellContent(args.Item, column, cellValue)); } } } // Raise the event to give a chance to external listeners to modify row clipboard content (e.ClipboardRow) if (CopyingRowClipboardContent != null) { CopyingRowClipboardContent(this, args); } }
/// <summary> /// Handles the case where a 'Copy' key ('C' or 'Insert') has been pressed. If pressed in combination with /// the control key, and the necessary prerequisites are met, the DataGrid will copy its contents /// to the Clipboard as text. /// </summary> /// <returns>Whether or not the DataGrid handled the key press.</returns> private bool ProcessCopyKey() { bool ctrl, shift, alt; KeyboardHelper.GetMetaKeyState(out ctrl, out shift, out alt); if (ctrl && !shift && !alt && this.ClipboardCopyMode != DataGridClipboardCopyMode.None && this.SelectedItems.Count > 0) { StringBuilder textBuilder = new StringBuilder(); if (this.ClipboardCopyMode == DataGridClipboardCopyMode.IncludeHeader) { DataGridRowClipboardEventArgs headerArgs = new DataGridRowClipboardEventArgs(null, true); foreach (DataGridColumn column in this.ColumnsInternal.GetVisibleColumns()) { headerArgs.ClipboardRowContent.Add(new DataGridClipboardCellContent(null, column, column.Header)); } this.OnCopyingRowClipboardContent(headerArgs); textBuilder.Append(FormatClipboardContent(headerArgs)); } for (int index = 0; index < this.SelectedItems.Count; index++) { object item = this.SelectedItems[index]; DataGridRowClipboardEventArgs itemArgs = new DataGridRowClipboardEventArgs(item, false); foreach (DataGridColumn column in this.ColumnsInternal.GetVisibleColumns()) { object content = column.GetCellValue(item, column.ClipboardContentBinding); itemArgs.ClipboardRowContent.Add(new DataGridClipboardCellContent(item, column, content)); } this.OnCopyingRowClipboardContent(itemArgs); textBuilder.Append(FormatClipboardContent(itemArgs)); } string text = textBuilder.ToString(); if (!string.IsNullOrEmpty(text)) { try { Clipboard.SetText(text); } catch (SecurityException) { // We will get a SecurityException if the user does not allow access to the clipboard. } return true; } } return false; }
protected virtual new void OnCopyingRowClipboardContent(DataGridRowClipboardEventArgs args) { Contract.Requires(args != null); }
/// <summary> /// This method formats a row (specified by a DataGridRowClipboardEventArgs) into /// a single string to be added to the Clipboard when the DataGrid is copying its contents. /// </summary> /// <param name="e">DataGridRowClipboardEventArgs</param> /// <returns>The formatted string.</returns> private string FormatClipboardContent(DataGridRowClipboardEventArgs e) { StringBuilder text = new StringBuilder(); for (int cellIndex = 0; cellIndex < e.ClipboardRowContent.Count; cellIndex++) { DataGridClipboardCellContent cellContent = e.ClipboardRowContent[cellIndex]; if (cellContent != null) { text.Append(cellContent.Content); } if (cellIndex < e.ClipboardRowContent.Count - 1) { text.Append('\t'); } else { text.Append('\r'); text.Append('\n'); } } return text.ToString(); }
/// <summary> /// This method raises the CopyingRowClipboardContent event. /// </summary> /// <param name="e">Contains the necessary information for generating the row clipboard content.</param> protected virtual void OnCopyingRowClipboardContent(DataGridRowClipboardEventArgs e) { EventHandler<DataGridRowClipboardEventArgs> handler = this.CopyingRowClipboardContent; if (handler != null) { handler(this, e); } }
private void dataGrid1_CopyingRowClipboardContent(object sender, DataGridRowClipboardEventArgs e) { var str = $"{e.Item}"; MessageBox.Show(str); }
private void SurveyView_CopyingRowClipboardContent(object sender, DataGridRowClipboardEventArgs e) { //Clipboard.SetData(DataFormats.Text, this.SurveyView.SelectedIndex); var nek = Clipboard.GetText(); this.CopySelectedStations = new List<Station>(this.SurveyView.SelectedItems.Cast<Station>().ToList()); }
protected virtual new void OnCopyingRowClipboardContent(DataGridRowClipboardEventArgs args) { Contract.Requires(args != null); }