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

How to deselect the text in a GridViewTextBoxColumn

1 Answer 123 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 23 May 2012, 08:38 PM
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);
    }
}

1 Answer, 1 is accepted

Sort by
0
Svett
Telerik team
answered on 28 May 2012, 11:11 AM
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 >>
Tags
GridView
Asked by
Michael
Top achievements
Rank 1
Answers by
Svett
Telerik team
Share this question
or