Completed
Last Updated: 17 Sep 2013 05:45 by ADMIN
ADMIN
Georgi I. Georgiev
Created on: 17 Sep 2013 05:45
Category: GridView
Type: Bug Report
0
FIX. RadGridView - Setting NewRowEnterKeyMode to EnterMovesToNextCell fires CellValidate twice
To reproduce use this code:

public partial class Form1 : Form
{
    RadGridView radGridView1 = new RadGridView();

    public Form1()
    {
        InitializeComponent();
        this.Controls.Add(this.radGridView1);
        this.radGridView1.Dock = DockStyle.Fill;

        this.radGridView1.AutoGenerateColumns = false;
        this.radGridView1.TableElement.RowHeight = 30;
        this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
        this.radGridView1.AddNewRowPosition = SystemRowPosition.Bottom;
        this.radGridView1.EnterKeyMode = RadGridViewEnterKeyMode.EnterMovesToNextCell;
        this.radGridView1.NewRowEnterKeyMode = RadGridViewNewRowEnterKeyMode.EnterMovesToNextCell;
        this.radGridView1.BeginEditMode = Telerik.WinControls.RadGridViewBeginEditMode.BeginEditOnKeystrokeOrF2;
        this.radGridView1.EnableGrouping = false;
        this.radGridView1.EnableAlternatingRowColor = true;
        this.radGridView1.AllowAddNewRow = true;
        this.radGridView1.AllowEditRow = true;
        this.radGridView1.AllowDeleteRow = true;
        this.radGridView1.EnableFiltering = true;
        this.radGridView1.EnableSorting = true;

        ArrayList arrayList = new ArrayList();

        arrayList.Add(new Person() { Name = "Jack", Family = "J..." });
        arrayList.Add(new Person() { Name = "Bob", Family = "B..." });
        arrayList.Add(new Person() { Name = "Dani", Family = "D..." });

        radGridView1.Columns.Add("ColName", "Name", "Name");
        radGridView1.Columns.Add("ColFamily", "Family", "Family");

        radGridView1.DataSource = arrayList;

        this.radGridView1.CellValidating +=radGridView1_CellValidating;

    }

    private void radGridView1_CellValidating(object sender, CellValidatingEventArgs e)
    {
        GridViewDataColumn column = e.Column as GridViewDataColumn;
        if ((e.Row is GridViewDataRowInfo || e.Row is GridViewNewRowInfo) && column != null
            && (column.Name == "ColName"))
        {
            if (e.Value == null || ((string)e.Value).Trim() == "") if (string.IsNullOrEmpty
                ((string)e.Value) || ((string)e.Value).Trim() == string.Empty || string.IsNullOrWhiteSpace

                ((string)e.Value))
                {
                    if (e.ActiveEditor != null)
                    {
                        MessageBox.Show("Please enter your name");
                        e.Cancel = true;
                    }
                }
        }
    }
}

public class Person
{

    private string name;
    private string family;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public string Family
    {
        get { return family; }
        set { family = value; }
    }
}

Try to add a new row by clicking the cell of the first column and leaving it empty. You will see the messagebox twice.

Workaround:

public class MyGridRowBehavior : GridNewRowBehavior
{
    protected override bool ProcessEnterKey(KeyEventArgs keys)
    {
        GridViewNewRowInfo newRowInfo = (GridViewNewRowInfo)this.GridViewElement.CurrentRow;

        if (this.GridViewElement.NewRowEnterKeyMode == RadGridViewNewRowEnterKeyMode.EnterMovesToNextCell)
        {
            bool editorClosed = !this.IsInEditMode;
            if (this.IsInEditMode)
            {
                if (this.IsOnLastCell())
                {
                    if (newRowInfo != null)
                    {
                        newRowInfo.GetType().GetMethod("DeferUserAddedRow", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).Invoke(newRowInfo, null);
                    }

                    editorClosed = GridViewElement.EndEdit();
                }
                else
                {
                    editorClosed = GridViewElement.CloseEditor();
                }
            }

            if (editorClosed)
            {
                this.Navigator.SelectNextColumn();
                this.GridViewElement.BeginEdit();
            }

            if (this.IsInEditMode && this.GridViewElement.CurrentRow is GridViewNewRowInfo &&
                this.GridViewElement.BeginEditMode != RadGridViewBeginEditMode.BeginEditProgrammatically)
            {
                return this.GridViewElement.BeginEdit();
            }

            if (newRowInfo != null)
            {
                newRowInfo.GetType().GetMethod("RaiseUserAddedRow", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).Invoke(newRowInfo, null);
            }

            return false;
        }
        
        return base.ProcessEnterKey(keys);
    }
}

((BaseGridBehavior)this.radGridView1.GridBehavior).UnregisterBehavior(typeof(GridViewNewRowInfo));
((BaseGridBehavior)this.radGridView1.GridBehavior).RegisterBehavior(typeof(GridViewNewRowInfo), new MyGridRowBehavior());
0 comments