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

Overriding the Copy and Paste menus?

3 Answers 229 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Dave Galligher
Top achievements
Rank 2
Dave Galligher asked on 19 Jan 2011, 01:24 AM
I've had to override the default menus for individual cells in the application being developed, the design department wanted to keep the functionality of the copy and paste functionality of the original, since I've overriden the context menu this is no longer using the default menus provided by Telerik.

Of course I've created the menus in the context menu created, wired up the event handler for the copy menu and for some reason the clipboard just isn't retaining the data. Here is the code to copy:

            double cellValue = Convert.ToDouble(radGridView.CurrentRow.Cells[radGridView.CurrentColumn.Index].Value);
            Clipboard.SetDataObject(cellValue);

The menu is only available in columns of type GridViewDecimalColumns, hence the Convert.ToDouble on getting the value.

Pasting is done a similar way:

            IDataObject iData = Clipboard.GetDataObject();
            if (iData.GetDataPresent(DataFormats.Text))
                radGridView.CurrentRow.Cells[radGridView.CurrentColumn.Index].Value =
                    Convert.ToDouble(iData.GetData(DataFormats.Text));

However neither method works, copy or paste. The functionality is simple, copy a cell - paste a value. Data needs to be able to copied to other programs like Excel, Word, etc. Clipboard is available on the system, code does not error out, but for some reason the value is just not there.

Any assistance would be appreciated. Thank you in advance.

3 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 19 Jan 2011, 08:33 AM
Hello Dave,

Please take a look at the solution i have provided here.

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
Telerik WinForms MVP
0
Emanuel Varga
Top achievements
Rank 1
answered on 19 Jan 2011, 09:12 AM
Or a full example as follows:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Telerik.WinControls.UI;
 
public partial class Form1 : Form
{
    private RadGridView radGridView1;
    public Form1()
    {
        InitializeComponent();
        this.Controls.Add(radGridView1 = new RadGridView());
        radGridView1.Dock = DockStyle.Fill;
        radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
        radGridView1.ContextMenuOpening += new ContextMenuOpeningEventHandler(radGridView1_ContextMenuOpening);
        radGridView1.DataSource = new List<Test> { new Test(1, "Name1"), new Test(2, "Name2") };
    }
 
    void radGridView1_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
    {
        var dataCell = e.ContextMenuProvider as GridDataCellElement;
        if (dataCell == null)
        {
            return;
        }
        if (dataCell.ColumnInfo is GridViewDecimalColumn)
        {
            e.ContextMenu.Items.Clear();
            var copyItem = new RadMenuItem("Copy");
            copyItem.Click+=new EventHandler(copyItem_Click);
            var pasteItem = new RadMenuItem("Paste");
            pasteItem.Click+=new EventHandler(pasteItem_Click);
            e.ContextMenu.Items.AddRange(copyItem, pasteItem);
        }
    }
 
    void copyItem_Click(object sender, EventArgs e)
    {
        Clipboard.SetDataObject(double.Parse(radGridView1.CurrentCell.Value.ToString()));
    }
 
    void pasteItem_Click(object sender, EventArgs e)
    {
        if (radGridView1.CurrentCell == null)
        {
            return;
        }
 
        IDataObject iData = Clipboard.GetDataObject();
        if (iData.GetDataPresent(typeof(double)))
        {
            radGridView1.CurrentCell.Value =
                Convert.ToDouble(iData.GetData(typeof(double)));
        }
    }
}
 
public class Test
{
    public int Id { get; set; }
    public string Name { get; set; }
 
    public Test(int id, string name)
    {
        this.Id = id;
        this.Name = name;
    }
}

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
Telerik WinForms MVP
0
Accepted
Dave Galligher
Top achievements
Rank 2
answered on 19 Jan 2011, 05:09 PM
Thank you, but that did not work for me. This did however:

        void paste_Click(object sender, EventArgs e)
        {
            if (radGridView.CurrentCell == null)
                return;

            if (radGridView.CurrentCell.ColumnIndex >= 4)
            {
                IDataObject iData = Clipboard.GetDataObject();
                if (iData.GetDataPresent(DataFormats.Text))
                    radGridView.CurrentCell.Value =
                        Convert.ToDouble(iData.GetData(DataFormats.Text));
            }
        }

        void copyMenu_Click(object sender, EventArgs e)
        {
            string value = radGridView.CurrentCell.Value.ToString();
            Clipboard.SetDataObject(value);
        }

Tags
GridView
Asked by
Dave Galligher
Top achievements
Rank 2
Answers by
Emanuel Varga
Top achievements
Rank 1
Dave Galligher
Top achievements
Rank 2
Share this question
or