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

GridView CellValidating event fires more than once

2 Answers 354 Views
GridView
This is a migrated thread and some comments may be shown as answers.
kh Wpl
Top achievements
Rank 1
kh Wpl asked on 12 Sep 2013, 01:23 PM


Hi Everyone,

I have a problem with CellValidating event.

GridView CellValidating event fires more than once when  NewRowEnterKeyMode property sets  to RadGridViewNewRowEnterKeyMode.EnterMovesToNextCell.
 
I have messagebox in CellValidating event to check the value could not be empty. 
when I try press enter key to leave cell, CellValidating event fires Twice. While there is a need a time validate. as a result messgebox shown Twice

please pay attention only when

1. NewRowEnterKeyMode sets  to RadGridViewNewRowEnterKeyMode.EnterMovesToNextCell
2. Press Enter Key to move next cell ( Tab Key behavior is great)


I am very grateful everyone help me.
Thank You.



2 Answers, 1 is accepted

Sort by
0
kh Wpl
Top achievements
Rank 1
answered on 12 Sep 2013, 09:21 PM

For Example in this sample : (click on new add row , on first column without input any data press enter to try leave column)


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls.UI;
using System.Collections;

namespace Example
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            radGridView1.AutoGenerateColumns = false;
            radGridView1.TableElement.RowHeight = 30;
            radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
            radGridView1.AddNewRowPosition = SystemRowPosition.Bottom;
            radGridView1.EnterKeyMode = RadGridViewEnterKeyMode.EnterMovesToNextCell;
            radGridView1.NewRowEnterKeyMode = RadGridViewNewRowEnterKeyMode.EnterMovesToNextCell;
            radGridView1.BeginEditMode = Telerik.WinControls.RadGridViewBeginEditMode.BeginEditOnKeystrokeOrF2;
            radGridView1.EnableGrouping = false;
            radGridView1.EnableAlternatingRowColor = true;
            radGridView1.AllowAddNewRow = true;
            radGridView1.AllowEditRow = true;
            radGridView1.AllowDeleteRow = true;
            radGridView1.EnableFiltering = true;
            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;
        }

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





0
George
Telerik team
answered on 17 Sep 2013, 11:13 AM
Hello,

Thank you for contacting us.

I was able to reproduce the issue you are describing. I have logged it in our Public Issue Tracking System. You can find it here - PITS Issue. Feel free to add your comment/vote and subscribe for changes.

For the time being, you can use the following grid behavior as a 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);
    }
}

You can register it as follows:
((BaseGridBehavior)this.radGridView1.GridBehavior).UnregisterBehavior(typeof(GridViewNewRowInfo));
((BaseGridBehavior)this.radGridView1.GridBehavior).RegisterBehavior(typeof(GridViewNewRowInfo), new MyGridRowBehavior());

I have also updated your Telerik Points for the report.

I hope this helps.
 
Regards,
George
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
GridView
Asked by
kh Wpl
Top achievements
Rank 1
Answers by
kh Wpl
Top achievements
Rank 1
George
Telerik team
Share this question
or