Completed
Last Updated: 23 Apr 2014 12:56 by ADMIN
ADMIN
Plamen
Created on: 28 Dec 2012 10:29
Category: GridView
Type: Bug Report
0
FIX. RadGridView - CellValidated and CellValidating events are not fired when pasting multiple cells from excel
STEPS TO REPRODUCE:

1) Create an excel file with sample string values (for example 3x3):
| Value 1 | Value 2 | Value 3 |
| Value 4 | Value 5 | Value 6 |
| Value 7 | Value 8 | Value 9 |

2) Validate as double:

        private ErrorProvider errorProvider = new ErrorProvider();

        void gridView_CellValidated(object sender, CellValidatedEventArgs e)
        {
            if (e.Value == null)
            {
                return;
            }
 
            var cell = e.Row.Cells[e.ColumnIndex];
 
            double value = 0;
            if (double.TryParse(e.Value.ToString(), out value))
            {
                cell.ErrorText = string.Empty;
                errorProvider.SetError(gridView, string.Empty);
            }
            else
            {
                cell.ErrorText = "Wrong input format";
                errorProvider.SetError(gridView, "Wrong input format");
 
            }
        }

3) Copy the cells from excel and paste them in the grid

Expected result: Validate all cells
Actual result: Only the current cell is being validated and the CellValidated and CellValidating events are not fired for the other cells

WORKAROUND: 

public class CustomGridBehavior : BaseGridBehavior
        {
            Form1 form;

            public CustomGridBehavior(Form1 form)            
            {
                this.form = form;
            }

            public override bool ProcessKey(KeyEventArgs keys)
            {
                if (keys.KeyCode == Keys.V && keys.Control)
                {
                    GridControl.MasterTemplate.Paste();
                    foreach (GridViewRowInfo row in GridControl.ChildRows)
                    {
                        form.ValidateCell(row.Cells[GridControl.CurrentColumn.Index]);
                    }
                    return true;
                }
                return base.ProcessKey(keys);
            }
        }

        void gridView_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
        {
            foreach (RadMenuItem item in e.ContextMenu.Items)
            {
                if (item.Text == "Paste")
                {
                    e.ContextMenu.Items.Remove(item);
                    break;
                }
            }
            RadMenuItem myPasteItem = new RadMenuItem("Paste");
            myPasteItem.Click += new EventHandler(myPasteItem_Click);
            e.ContextMenu.Items.Add(myPasteItem);
        }

        void myPasteItem_Click(object sender, EventArgs e)
        {
            gridView.MasterTemplate.Paste();
            foreach (GridViewRowInfo row in gridView.ChildRows)
            {
                ValidateCell(row.Cells[gridView.CurrentColumn.Index]);
            }
        }
0 comments