Pasting a row with readonly columns behaves incorrectly

4 posts, 1 answers
  1. Karl
    Karl avatar
    35 posts
    Member since:
    Dec 2013

    Posted 10 Sep 2015 Link to this post

    If I have a row with columns marked as read only and try to paste to it, the column will be skipped causing an undesired behavior.  Is this a bug?  Is there a workaround for this?  I have attached screen shots demonstrating the behavior. Notice that my last column is blank after the paste.

     

     

     

     

  2. Hristo Merdjanov
    Admin
    Hristo Merdjanov avatar
    712 posts

    Posted 15 Sep 2015 Link to this post

    Hello Karl,

    Thank you for writing.

    Setting particular columns to be read-only affects the entire RadGridView. You can copy rows having some oftheir cells in read-only columns and paste these rows to a new location, the behavior on my end is expected. The cells which are not read-only are successfully pasted while the others are not. Values are not disappearing from the other cells as well. Please see my code snippet below testing your scenario:
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
     
            this.radGridView1.DataSource = this.GetData();
            this.radGridView1.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;
     
            this.radGridView1.Columns[1].ReadOnly = true;
            this.radGridView1.Columns[2].ReadOnly = true;
            this.radGridView1.Columns[3].ReadOnly = true;
        }
     
        private DataTable GetData()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Age", typeof(int));
            dt.Columns.Add("Date", typeof(DateTime));
            dt.Columns.Add("Bool", typeof(bool));
            for (int i = 0; i < 10; i++)
            {
                dt.Rows.Add("Name " + i, i, DateTime.Now.AddMinutes(i), i % 2 == 0 ? true : false);
            }
     
            return dt;
        }
    }

    Alternatively you can handle the CellBeginEdit event and cancel it for certain columns. This will disable editing of the cells by users, however if they would like to copy and paste data, that would be possible: 
    private void radGridView1_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
    {
        if (e.Column.Index > 0)
        {
            e.Cancel = true;
        }
    }

    I am also sending you a gif file showing the result on my end.

    If you are still experiencing issues please send me a code snippet demonstrating your local setup and please specify what version of the controls you are using.

    I hope this helps. Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo Merdjanov
    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
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Karl
    Karl avatar
    35 posts
    Member since:
    Dec 2013

    Posted 15 Sep 2015 in reply to Hristo Merdjanov Link to this post

    Your example works, however if you move name to row 2 and try the same thing the Age value will be pasted into the Name column.

     Modified code is as follows:

    public Form1()
        {
        InitializeComponent();
        this.radGridView1.DataSource = this.GetData();
        this.radGridView1.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;
     
        this.radGridView1.Columns[0].ReadOnly = true;
        this.radGridView1.Columns[2].ReadOnly = true;
        this.radGridView1.Columns[3].ReadOnly = true;
     
        }
     
    private DataTable GetData()
        {
        DataTable dt = new DataTable();
        dt.Columns.Add("Age", typeof(int));
        dt.Columns.Add("Name", typeof(string));           
        dt.Columns.Add("Date", typeof(DateTime));
        dt.Columns.Add("Bool", typeof(bool));
        for (int i = 0; i < 10; i++)
            {
            dt.Rows.Add(i, "Name " + i, DateTime.Now.AddMinutes(i), i % 2 == 0 ? true : false);
            }
     
        return dt;
        }

  5. Answer
    Hristo Merdjanov
    Admin
    Hristo Merdjanov avatar
    712 posts

    Posted 17 Sep 2015 Link to this post

    Hello Karl,

    Thank you for writing back.

    When read-only columns are present in RadGridView, the Paste method searches for a not read-only cell and changes its value according to what is stored in the clipboard, starting from the first item. 

    You can customize this behavior by subscribing you RadGridView to the Paste event and in the handler work with the data in the clipboard. My code snippet below demonstrates how you can paste whole rows and have only the values in the not read-only columns changed: 
    private void radGridView1_Pasting(object sender, Telerik.WinControls.UI.GridViewClipboardEventArgs e)
    {
        if (Clipboard.ContainsData(DataFormats.Text))
        {
            string data = Clipboard.GetData(DataFormats.Text).ToString();
            if (data != string.Empty)
            {
                string[] dataArray = data.Split(new char[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);
                if (dataArray.Length == this.radGridView1.Columns.Count)
                {
                    for (int i = 0; i < this.radGridView1.CurrentRow.Cells.Count; i++)
                    {
                        if (!this.radGridView1.Columns[i].ReadOnly)
                        {
                            this.radGridView1.CurrentRow.Cells[i].Value = dataArray[i];
                        }
                    }
                }
                 
                e.Cancel = true;
            }
        }
    }

    Additional information is available in the following documentation article: Copy/Paste/Cut. I am also sending you a gif file showing the result on my end.

    I hope this helps. Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo Merdjanov
    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
Back to Top