This is a migrated thread and some comments may be shown as answers.

Copying multiple rows from GridView does not preserve linebreaks

5 Answers 453 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Doug
Top achievements
Rank 1
Doug asked on 05 Apr 2013, 04:11 PM
I am trying to enable copying from a GridView to paste into other programs, but I'm encountering multiple problems.

I have a GridView with MultiSelect = True, SelectionMode = FullRowSelect, and ClipboardCopyMode = EnableAlwaysIncludeHeaderText (though the same problems occurs with EnableWithoutHeaderText).

Problem #1:
When I select a single row, only the current cell is copied. It is pasted as: ColumnHeader<CrLf>CellValue

Expected result: Whole row should be copied.

Problem #2:
When I select multiple rows, copy and paste into another program (I've tried Notepad++ and Excel), the rows are all pasted as a single line. There are tab characters between the cell values, but no newline characters. Instead there is a tab character between the rows. This makes it impossible to paste into Excel -- all values are pasted into a single cell.

Expected result: Tabs between cells, newline between rows.

Problem #3:
To combat the above, I've tried capturing the "Copying" event to roll my own clipboard function:

Private Sub GridClipboardPrep(sender As System.Object, e As Telerik.WinControls.UI.GridViewClipboardEventArgs) _
    Handles GVComputerSearch.Copying
 
    Dim copyStr As String = ConvertSelectedDataToString(sender)
    Clipboard.SetText(copyStr)
End Sub
 
Private Function ConvertSelectedDataToString(grid As RadGridView) As String
    Dim strBuild As New System.Text.StringBuilder()
 
    Dim row As Integer = 0
    While row < grid.SelectedRows.Count
        Dim cell As Integer = 0
        While cell < grid.SelectedRows(row).Cells.Count
            strBuild.Append(grid.SelectedRows(row).Cells(cell).Value.ToString())
            strBuild.Append(vbTab)
            System.Math.Max(System.Threading.Interlocked.Increment(cell), cell - 1)
        End While
 
        strBuild.Append(System.Environment.NewLine)
        System.Math.Max(System.Threading.Interlocked.Increment(row), row - 1)
    End While
 
    Return strBuild.ToString()
End Function

But the end result is the same as Problem #2 above.

So I put a breakpoint at the end of Sub GridClipboardPrep and guess what? It runs three times! Furthermore, if I paste into Notepad while the program is paused at the breakpoint, it pastes correctly (though without the header row). After the program continues, paste looks like Problem #2 again.

5 Answers, 1 is accepted

Sort by
0
Julian Benkov
Telerik team
answered on 10 Apr 2013, 02:18 PM
Hello Doug,

1) The default logic copies only the selected cells during copy operation and will copy the full row if you use the context menu of row indicator column. Please review the attached screenshot. To always copy the full data row in the Clipboard you can use the new BeginRowCopy API of MasterTemplate:
gridView.MasterTemplate.BeginRowCopy();

2) I have tested the reported scenario and RadGridView successfully copies and pastes multiple selected rows in Notepad++ and Excel with the formatting information including the new lines. Please keep in mind that in order to support this behavior RadGridView copies the information in various formats like text, html, cvs. When a copy operation prepares these formats the Copying event will be raised for each one. Please review the example in code snippet and the attached video for details:
using System;
using System.Windows.Forms;
using Telerik.WinControls.UI;
 
namespace Lab.Grid
{
    public partial class GridCopyPaste : MainForm
    {
        private RadGridView gridView = new RadGridView();
 
        public GridCopyPaste()
        {
            InitializeComponent();
 
            gridView.Dock = DockStyle.Fill;
            gridView.Parent = this;
            gridView.BringToFront();
            gridView.MultiSelect = true;
            gridView.SelectionMode = GridViewSelectionMode.FullRowSelect;
            gridView.ClipboardPasteMode = GridViewClipboardPasteMode.EnableWithNotifications;
            gridView.Copying += gridView_Copying;
            gridView.Pasting += gridView_Pasting;
            gridView.MasterTemplate.BeginRowCopy();
        }
 
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
 
            gridView.ColumnCount = 6;
            gridView.RowCount = 10;
 
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 6; j++)
                {
                    this.gridView.Rows[i].Cells[j].Value = i + j;
                }
            }
        }
 
        void gridView_Copying(object sender, GridViewClipboardEventArgs e)
        {
            Console.WriteLine("Copying: {0}", e.Format);
        }
 
        void gridView_Pasting(object sender, GridViewClipboardEventArgs e)
        {
            if (e.Format == DataFormats.Html)
            {
                e.Cancel = true;
            }
            Console.WriteLine("Pasting: {0}", e.Format);
        }
    }
}

I hope this helps.

Regards,
Julian Benkov
the Telerik team
WinForms Q1 2013 boasts PivotGrid, PDF Viewer, Chart enhancements and more. Check out all of the latest highlights.
0
Doug
Top achievements
Rank 1
answered on 10 Apr 2013, 03:23 PM
Thanks for your detailed response. Unfortunately, I still have issues:

  • If AllowDeleteRow = False, then the cut, copy & paste items disappear from the context menu in the row indicator column. This is incorrect behavior and prevents copying a whole row. (Also, I do not want the user to have the ability to delete a row.)
  • If SelectionMode = CellSelect (and AllowDeleteRow = True), then I can copy a paste a full row with headers, but only one row at a time. This does not allow me to select more than one full row.
  • If SelectionMode = FullRowSelect, then copy and paste still smooshes all the data into a single line.

0
Julian Benkov
Telerik team
answered on 15 Apr 2013, 09:51 AM
Hi Doug,

1) The
cut operation is not valid in this case when the AllowDeleteRow is false. We will extend this functionality to enable other operations in this scenario. To workaround the current processing of you can customize the context menu using this KB Article.

2,3) To support full row copying for multiple rows you must also enable the MultiSelect and call the BeginCopyRow:
gridView.MultiSelect = true;
gridView.MasterTemplate.BeginRowCopy();

If you continue experiencing the issues with copy / paste operations, please send me a sample application with your scenario to investigate it locally and find best solution for the case.

Thank you for your time and cooperation.

Greetings,
Julian Benkov
the Telerik team
WinForms Q1 2013 boasts PivotGrid, PDF Viewer, Chart enhancements and more. Check out all of the latest highlights.
0
SHAHROKH
Top achievements
Rank 2
answered on 12 Aug 2015, 06:42 AM

Hi,

How can i get values in multi selection Rad Grid View?

Please help me.

0
Stefan
Telerik team
answered on 12 Aug 2015, 08:48 AM
Hi ,

RadGridView features the SelectedRows and SelectedCells properties, from there you can get the currently selected items. 

On a side note, may I please ask you to avoid mixing different subjects in one thread as we are trying to keep the forums more easy to read. If you can't find a thread for the precise question, you can always open a new one. Thank you for the understanding.

Regards,
Stefan
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
GridView
Asked by
Doug
Top achievements
Rank 1
Answers by
Julian Benkov
Telerik team
Doug
Top achievements
Rank 1
SHAHROKH
Top achievements
Rank 2
Stefan
Telerik team
Share this question
or