Overriding the Copy and Paste menus?

4 posts, 1 answers
  1. Dave Galligher
    Dave Galligher avatar
    37 posts
    Member since:
    May 2010

    Posted 18 Jan 2011 Link to this post

    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.
  2. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 19 Jan 2011 Link to this post

    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
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 19 Jan 2011 Link to this post

    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
  5. Answer
    Dave Galligher
    Dave Galligher avatar
    37 posts
    Member since:
    May 2010

    Posted 19 Jan 2011 Link to this post

    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);
            }

Back to Top