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

Custom Editor - Missing first Keystroke

1 Answer 73 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Richard
Top achievements
Rank 1
Richard asked on 22 Mar 2016, 12:15 PM

I am evaluating RadGridview. I need to create lots of custom editors for my upcoming project. To begin with, I just created a simple texteditor with the following code (I referred you demo code for this) .The issue is, the control losing the first key stroke. Suppose I start to edit a column by typing "John" then the column only showing "ohn". 

Did I miss something ??

 

class MyTextEditor : BaseGridEditor
   {
       public RadFormProductList ListForm { get; set; }
       protected override RadElement CreateEditorElement()
       {
           var editor = new MyTextEditorElement();
           return editor;
       }
       public override object Value
       {
           get
           {
               MyTextEditorElement editor = (MyTextEditorElement)this.EditorElement;
               return editor.Text;
           }
           set
           {
               MyTextEditorElement editor = (MyTextEditorElement)this.EditorElement;
               if (value != null && value != DBNull.Value)
               {
                   editor.Text = value.ToString();
               }
               else
               {
                   editor.Text = "";
               }
           }
       }
      
   }
 
   public class MyTextEditorElement : RadTextBoxElement
   {
       public MyTextEditorElement()
       {
           this.BackColor = Color.White;
           this.ShowBorder = false;
       }
 
       protected override Type ThemeEffectiveType
       {
           get
           {
               return typeof(RadTextBoxEditorElement);
           }
       }
 
   }

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 24 Mar 2016, 08:58 AM
Hello Richard,

Thank you for writing.

RadGridView manages user mouse and keyboard input over its rows by GridRowBehavior. Depending on the row type, RadGridView introduces different behaviors. Thus, typing while the grid is not in edit mode, the relevant editor is activated and initialized with the entered text. This is performed in the ProcessAlphaNumericKey method of the GridRowBehavior when the editor is RadTextBoxEditor. However, your custom editor inherits BaseGridEditor. That is why the first character is not stored. You can find below a sample code snippet demonstrating how to initialize the custom editor with the entered text:
public Form1()
{
    InitializeComponent();
    this.radGridView1.EditorRequired += radGridView1_EditorRequired;
 
    //register the custom row  behavior
    BaseGridBehavior gridBehavior = radGridView1.GridBehavior as BaseGridBehavior;
    gridBehavior.UnregisterBehavior(typeof(GridViewDataRowInfo));
    gridBehavior.RegisterBehavior(typeof(GridViewDataRowInfo), new CustomGridDataRowBehavior());
}
 
public class CustomGridDataRowBehavior : GridDataRowBehavior
{
    protected override bool ProcessAlphaNumericKey(KeyPressEventArgs keys)
    {
        bool result = base.ProcessAlphaNumericKey(keys);
        if (this.IsInEditMode &&
            (this.BeginEditMode == RadGridViewBeginEditMode.BeginEditOnKeystroke ||
             this.BeginEditMode == RadGridViewBeginEditMode.BeginEditOnKeystrokeOrF2))
        {
            if (this.GridViewElement.ActiveEditor is MyTextEditor)
            {
                this.GridViewElement.ActiveEditor.Value = keys.KeyChar;
                if (this.GridViewElement.IsInEditMode)
                {
                    MyTextEditor textBox = (MyTextEditor)this.GridViewElement.ActiveEditor;
                    RadTextBoxItem textBoxItem = ((RadTextBoxElement)textBox.EditorElement).TextBoxItem;
                    textBoxItem.SelectionStart = 1;
                    textBoxItem.SelectionLength = 0;
                }
 
                return true;
            }
        }
        return result;
    }
}
 
private void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)
{
    e.Editor = new MyTextEditor();
}
 
private void Form1_Load(object sender, EventArgs e)
{
    // TODO: This line of code loads data into the 'nwindDataSet.Customers' table. You can move, or remove it, as needed.
    this.customersTableAdapter.Fill(this.nwindDataSet.Customers);
}
 
class MyTextEditor : BaseGridEditor
{
    //public RadFormProductList ListForm { get; set; }
    protected override RadElement CreateEditorElement()
    {
        var editor = new MyTextEditorElement();
        return editor;
    }
    public override void BeginEdit()
    {
        base.BeginEdit();
        MyTextEditorElement editor = (MyTextEditorElement)this.EditorElement;
        editor.TextBoxItem.TextBoxControl.Focus();
    }
    public override object Value
    {
        get
        {
            MyTextEditorElement editor = (MyTextEditorElement)this.EditorElement;
            return editor.Text;
        }
        set
        {
            MyTextEditorElement editor = (MyTextEditorElement)this.EditorElement;
            if (value != null && value != DBNull.Value)
            {
                editor.Text = value.ToString();
            }
            else
            {
                editor.Text = "";
            }
        }
    }
}
 
public class MyTextEditorElement : RadTextBoxElement
{
    public MyTextEditorElement()
    {
        this.BackColor = Color.White;
        this.ShowBorder = false;
    }
 
    protected override Type ThemeEffectiveType
    {
        get
        {
            return typeof(RadTextBoxEditorElement);
        }
    }
}

I hope this information helps. Should you have further questions I would be glad to help.
 
Regards,
Dess
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Tags
GridView
Asked by
Richard
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or