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

GridView - Copy Data From Child Template to Clipboard

1 Answer 72 Views
GridView
This is a migrated thread and some comments may be shown as answers.
The Kawa Dev
Top achievements
Rank 1
The Kawa Dev asked on 22 Nov 2013, 10:42 PM
Hello,

Per this thread, I see that we now have proper copy functionality in the radgridview control:
http://www.telerik.com/community/forums/winforms/gridview/copying-multiple-rows-from-gridview-does-not-preserve-linebreaks.aspx


I am able to successfully use

grid.MasterTemplate.BeginRowCopy()


My question is this:
How would I go about using this functionality in a hierarchical view, where the user wishes to copy data in the "child" template.
grid.Templates(0)
  does not have a corresponding "BeginRowCopy"

When I call grid.MasterTemplate.BeginRowCopy()  I receive repeated errors stating:

Property accessor 'Name' on object 'class name' threw the following exception: 'Object does not match target type.'

To clarify this error, the parent table contains a column (and property corresponding to the object) called "Name".
The child templates data does not contain a property of that name.  The obvious conclusion is that grid.MasterTemplate.BeginRowCopy() only works for the master template and does not operate on the child template.

Any suggestions on how to accomplish this?

Update:  Ctrl-C exhibits the same issue.

Thank you.

PS: It is a shame that the only documentation on the matter came from another forum post.  It would be helpful to see Copy/Paste functionality fleshed out in the WinForm Documentaiton or KB articles.


1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 27 Nov 2013, 05:38 PM
Hello Jordan,

Thank you for contacting Telerik Support.

Currently, Copy function for the child template is not supported. However, we have this issue logged in our Public Issue Tracking System - PITS. You can track its progress, subscribe for status changes and add your vote/comment to it on the following link - PITS issue.

The possible workaround that I can propose handle manually Copy operation:
radGridView1.ContextMenuOpening += radGridView1_ContextMenuOpening;

string clipBoard = string.Empty;
RadGridView contextMenuInvoker;
 
private void radGridView1_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
{
    RadGridView grid = sender as RadGridView;
    if (grid.CurrentCell is GridDataCellElement || grid.CurrentCell is GridTableBodyElement)
    {
        if (grid.CurrentRow.ViewTemplate != grid.MasterGridViewTemplate && !(grid.CurrentCell.RowInfo is GridViewNewRowInfo))
        {
            RadMenuItem itemCopy = new RadMenuItem();
            itemCopy.Text = "Copy Row(s)";
            itemCopy.Click += new EventHandler(itemCopy_Click);
            e.ContextMenu.Items.RemoveAt(3);
            e.ContextMenu.Items.Insert(3, itemCopy);
            e.ContextMenu.Items.RemoveAt(4);
        }
        contextMenuInvoker = grid;
    }
}
 
void itemCopy_Click(object sender, EventArgs e) 
{
    CopyRows(contextMenuInvoker);
}
 
private void CopyRows(RadGridView radGridView)
{
    StringBuilder sb = new StringBuilder();
    foreach (GridViewRowInfo row in radGridView.SelectedRows)
    {
        int i = 0;
        while (i < row.Cells.Count)
        {
            if (i > 0)
            {
                sb.Append(",");
            }
            sb.Append(row.Cells[i].Value.ToString());
            i++;
        }
        sb.AppendLine(";");
    }
    clipBoard = sb.ToString();
    Clipboard.SetDataObject(clipBoard);
}

P.S. We will consider including Copy/Paste functionality in RadGridView help section.

I hope this information helps. Should you have further questions, I would be glad to help.

Regards,
Desislava
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
GridView
Asked by
The Kawa Dev
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or