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

Pasting a row with readonly columns behaves incorrectly

3 Answers 149 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Karl B
Top achievements
Rank 1
Karl B asked on 10 Sep 2015, 07:05 PM

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.

 

 

 

 

3 Answers, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 15 Sep 2015, 09:27 AM
Hello Karl,

Thank you for writing.

Setting particular columns to be read-only affects the entire RadGridView. You can copy rows having some 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
0
Karl B
Top achievements
Rank 1
answered on 15 Sep 2015, 06:09 PM

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

0
Accepted
Hristo
Telerik team
answered on 17 Sep 2015, 01:56 PM
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
Tags
GridView
Asked by
Karl B
Top achievements
Rank 1
Answers by
Hristo
Telerik team
Karl B
Top achievements
Rank 1
Share this question
or