How to deselect the text in a GridViewTextBoxColumn

2 posts, 0 answers
  1. Michael
    Michael avatar
    8 posts
    Member since:
    Feb 2012

    Posted 23 May 2012 Link to this post

    Telerik controls v2010.1.10.504

    The default behavior in a textbox column in a gridview is to have the text auto-selected when the cell is clicked. We have a requirement to override that behavior because typing can overwrite the entire contents, which our users don't want.

    Unfortunately, if I do it via the CellFormatting event, then a race condition occurs if I manually try to select all the text and it becomes difficult to "Select All" to copy the cell contents. I have the CellClick clearing the text fine, it's actually when the embedded control is clicked in the cell which I'm unable to override.

    I could only find the "EditorRequired" event as being fired when the GridViewTextBoxColumn is clicked (not the cell, but the embedded control in the cell), but at that time the ActiveEditor is not set, so I'm unable to access the underlying control and "Select(0,0)".

    Please tell me how to override the default behavior.

    Thank you.

    Here is a sample code slice from the CellClick handler so you can see what I'm doing:
    if ("GridViewTextBoxColumn" == ((Telerik.WinControls.UI.GridDataCellElement)(sender)).GridControl.CurrentColumn.GetType().Name)
    {
        RadTextBoxEditor rtbe = ((Telerik.WinControls.UI.GridDataCellElement)(sender)).GridControl.ActiveEditor as RadTextBoxEditor;
        if (null != rtbe)
        {
            RadTextBoxEditorElement tbElement = (RadTextBoxEditorElement)rtbe.EditorElement;
            tbElement.Select(0, 0);
        }
    }
  2. Svett
    Admin
    Svett avatar
    728 posts

    Posted 28 May 2012 Link to this post

    Hi Michael,

    You should extend the behavior of the default editor by creating a custom one. You can use the following code snippets to achieve that:

    public class CustomGridTextEditor : RadTextBoxEditor
    {
        public override void BeginEdit()
        {
            base.BeginEdit();
     
            RadTextBoxEditorElement element = this.EditorElement as RadTextBoxEditorElement;
            element.TextBoxItem.Select(0, 0);
        }
    }

    Then you should replace the default in the EditorRequired event:
    void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)
    {
        if (e.EditorType == typeof(RadTextBoxEditor))
        {
            e.EditorType = typeof(CustomGridTextEditor);
        }
    }

    I hope this helps.

    Kind regards,
    Svett
    the Telerik team
    RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
Back to Top