Completed
Last Updated: 17 Jul 2017 14:36 by ADMIN
ADMIN
Hristo
Created on: 28 Jun 2017 07:06
Category: GridView
Type: Bug Report
1
FIX. RadGridView - cannot paste data after read-only cells
How to reproduce
public partial class Form1 : RadForm
{
    public Form1()
    {
        InitializeComponent();

        GridViewTextBoxColumn textBoxColumn = new GridViewTextBoxColumn();
        textBoxColumn.Name = "Column";
        textBoxColumn.HeaderText = "Column";
        this.radGridView1.MasterTemplate.Columns.Add(textBoxColumn);

        GridViewTextBoxColumn textBoxColumn2 = new GridViewTextBoxColumn();
        textBoxColumn2.Name = "TextBoxColumn2";
        textBoxColumn2.HeaderText = "ReadOnlyColumn";
        this.radGridView1.MasterTemplate.Columns.Add(textBoxColumn2);

        for (int i = 0; i < 10; i++)
        {
            object v = i * 2;
            if (i % 3 == 0)
            {
                v = null;
            }

            this.radGridView1.Rows.Add(new object[] { i, v });
        }

        this.radGridView1.MultiSelect = true;
        foreach (var row in this.radGridView1.Rows)
        {
            foreach (var cell in row.Cells)
            {
                GridViewCellInfo cellInfo = cell as GridViewCellInfo;
                if (cellInfo != null && cellInfo.RowInfo.Index % 3 == 0 && cellInfo.ColumnInfo.Index == 1)
                {
                    cellInfo.ReadOnly = true;
                }
            }
        }
    }

}

Workaround:
public class MyRadGridView : RadGridView
{
    public override string ThemeClassName
    {
        get
        {
            return typeof(RadGridView).FullName;
        }
    }

    protected override RadGridViewElement CreateGridViewElement()
    {
        return new MyRadGridViewElement();
    }
}

internal class MyRadGridViewElement : RadGridViewElement
{
    protected override Type ThemeEffectiveType
    {
        get
        {
            return typeof(RadGridViewElement);
        }
    }

    protected override MasterGridViewTemplate CreateTemplate()
    {
        return new MyMasterGridViewTemplate();
    }
}

internal class MyMasterGridViewTemplate : MasterGridViewTemplate
{
    protected override void PasteDataToRow(List<string> rowData, GridViewRowInfo row)
    {
        {
            int colIndex = this.Owner.CurrentColumn.Index;
            int j = 0;

            while (j < rowData.Count && colIndex < this.CurrentView.ViewTemplate.ColumnCount)
            {
                GridViewColumn col = this.CurrentView.ViewTemplate.Columns[colIndex];
                if (col.IsVisible && !col.ReadOnly && !row.Cells[colIndex].ReadOnly)
                {

                    object value = rowData[j];

                    if (string.IsNullOrEmpty(rowData[j]))
                    {
                        value = null;
                    }
                    else if (this.CurrentView.ViewTemplate.Columns[colIndex].DataType == typeof(string))
                    {
                        GridViewTextBoxColumn textColumn = col as GridViewTextBoxColumn;

                        if (textColumn != null && textColumn.MaxLength > 0)
                        {
                            if (rowData[j].Length > textColumn.MaxLength)
                            {
                                value = rowData[j].Substring(0, textColumn.MaxLength);
                            }
                        }
                    }
                    else if (this.CurrentView.ViewTemplate.Columns[colIndex].DataType == typeof(DateTime))
                    {
                        try
                        {
                            value = DateTime.Parse(rowData[j], this.CurrentView.ViewTemplate.Columns[colIndex].FormatInfo);
                        }
                        catch { }
                    }
                    else if (this.CurrentView.ViewTemplate.Columns[colIndex].DataType == typeof(Color))
                    {
                        try
                        {
                            value = ColorTranslator.FromHtml(rowData[j]);
                        }
                        catch { }
                    }

                    if (this.ClipboardPasteMode == GridViewClipboardPasteMode.EnableWithNotifications)
                    {
                        CellValidatingEventArgs cellValidating = new CellValidatingEventArgs(row, col, value, row.Cells[colIndex].Value, null);
                        this.EventDispatcher.RaiseEvent<CellValidatingEventArgs>(EventDispatcher.CellValidating, this, cellValidating);

                        if (!cellValidating.Cancel)
                        {
                            row.Cells[colIndex].Value = value;

                            CellValidatedEventArgs cellValidated = new CellValidatedEventArgs(row, col, value);
                            this.EventDispatcher.RaiseEvent<CellValidatedEventArgs>(EventDispatcher.CellValidated, this, cellValidated);
                        }
                    }
                    else
                    {
                        row.Cells[colIndex].Value = value;
                    }

                    j++;
                }

                colIndex++;
            }
        }
    }
}


(Total attached files size should be smaller than 20mb. Allowed extensions: .zip, .rar, .jpg, .png, .gif)
0 comments