howto Copy/Paste text from a readonly column?

7 posts, 1 answers
  1. Diederik
    Diederik avatar
    12 posts
    Member since:
    Feb 2009

    Posted 26 Nov 2010 Link to this post

    Hi,

    I am looking for an easy way to let the user select and copy/paste text from a readonly textboxcolumn in a grid. How can this be achieved?

    Best regards,
    Diederik.
  2. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 26 Nov 2010 Link to this post

    Hello,

    Please take a look at the solution i have provided here, you can easily implement this operations using a context menu, or you could handle the KeyDown event in a custom grid behavior to send the text to the clipboard.

    Please let me know if you need an example for the second option.

    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. Diederik
    Diederik avatar
    12 posts
    Member since:
    Feb 2009

    Posted 26 Nov 2010 Link to this post

    Hi Emanuel,

    Could you provide me the example of the second option?

    Thanks for your fast response.
    Diederik.
  5. Answer
    Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 26 Nov 2010 Link to this post

    Hello again,

    Please take a look at the following example:

    using System.ComponentModel;
    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.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
            radGridView1.Dock = DockStyle.Fill;
            radGridView1.DataSource = new TestsCollection();
            radGridView1.ReadOnly = true;
            radGridView1.GridBehavior = new CustomGridBehavior();
        }
     
        private class CustomGridBehavior : BaseGridBehavior
        {
            public override bool ProcessKeyDown(KeyEventArgs keys)
            {
                if (keys.KeyData == (Keys.Control | Keys.C))
                {
                    Clipboard.SetText(this.GridControl.CurrentCell.Value.ToString());
                }
     
                return base.ProcessKeyDown(keys);
            }
        }
    }
     
    public class Test
    {
        public int Id
        {
            get;
            set;
        }
     
        public string Name
        {
            get;
            set;
        }
    }
     
    public class TestsCollection : BindingList<Test>
    {
        public TestsCollection()
        {
            for (int i = 0; i < 10; i++)
            {
                this.Add(new Test
                {
                    Id = i,
                    Name = "Name" + i
                });
            }
        }
    }

    This will just copy the text from the current cell, about the pasting, where do you want to paste that value because i'm guessing not in the readonly cells.

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

    Best Regards,
    Emanuel Varga
    Telerik WinForms MVP
  6. Michal Zámec
    Michal Zámec avatar
    5 posts
    Member since:
    May 2010

    Posted 19 May 2011 Link to this post

    Hi,

    those examples you've provided are only able to select (copy) whole value of the cell. Is there a way to implement behavior, which will allow user to select just part of the text in read-only grid using mouse? I need to implement access-like behavior, where you easily could select part of the text in grid cell and use that selected text for other actions (for example fliter by context menu etc).

    Thans
  7. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 20 May 2011 Link to this post

    Hello Michal,

    In order to achieve your desired behavior you first will need to be able to show a textbox on the cell, in order to select part of that text, i believe if you are ok with doing things this way, it could be possible, BUT, you cannot rely on the grid being readonly anymore you will have to threat all different editors to show up, but to negate any edit operation (based on each editor type).

    So in conclusion, it is doable, but it will take more work for some editors, for textbox it's easy, for datetimepicker & combo not so much.

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

    Best Regards,
    Emanuel Varga

    Telerik WinForms MVP
  8. Jack
    Admin
    Jack avatar
    2333 posts

    Posted 25 May 2011 Link to this post

    Hello Michal Zámec,

    Currently we do not provide such functionality in RadGridView. You have to implement it in the way Emanuel suggested. However, this is a nice feature and I added it in our issue tracking system as a feature request. I updated also your Telerik points. We will consider implementing it in a future version of our controls.

    If you have any other questions, do not hesitate to ask.
     
    All the best,
    Jack
    the Telerik team
    Q1’11 SP1 of RadControls for WinForms is available for download; also available is the Q2'11 Roadmap for Telerik Windows Forms controls.
Back to Top
UI for WinForms is Visual Studio 2017 Ready