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

Accept Tab in GridView Column

1 Answer 209 Views
GridView
This is a migrated thread and some comments may be shown as answers.
wima
Top achievements
Rank 1
wima asked on 23 Dec 2009, 11:29 AM
Hello
I want to enter Tabs and Linebreaks in a textbox column. I tryed this, but it does not work.
private void radGridView1_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)  
{  
    RadTextBoxEditor editor = radGridView1.ActiveEditor as RadTextBoxEditor;  
    editor.AcceptsReturn = true;  
    editor.AcceptsTab = true;  
When I press the tab key, the cursor goes to the next column.

Greetings,
wima

1 Answer, 1 is accepted

Sort by
0
Jack
Telerik team
answered on 29 Dec 2009, 09:53 AM
Hello wima,

The build-in textbox editor in RadGridView doesn't support these features. We know about this limitation and we plan to improve the editor in one of our upcoming releases. Nevertheless, you can achieve the desired behavior by using a custom editor. I prepared a sample. Please consider the following code snippet:

void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)
{
    if (e.EditorType == typeof(RadTextBoxEditor))
    {
        e.EditorType = typeof(MyTextBoxEditor);
    }
}
 
public class MyTextBoxEditor : BaseGridEditor
{
    public override object Value
    {
        get
        {
            MyTextBoxEditorElement editorElement = (MyTextBoxEditorElement)this.EditorElement;
            return editorElement.HostedControl.Text;
        }
        set
        {
            MyTextBoxEditorElement editorElement = (MyTextBoxEditorElement)this.EditorElement;
            if (value == null || value == DBNull.Value)
            {
                editorElement.HostedControl.Text = string.Empty;
            }
            else
            {
                editorElement.HostedControl.Text = value.ToString();
            }
        }
    }
 
    public override void BeginEdit()
    {
        base.BeginEdit();
        MyTextBoxEditorElement editorElement = (MyTextBoxEditorElement)this.EditorElement;
        editorElement.HostedControl.BackColor = Color.White;
    }
 
    protected override RadElement CreateEditorElement()
    {
        return new MyTextBoxEditorElement();
    }
}
 
public class MyTextBoxEditorElement : RadHostItem
{
    public MyTextBoxEditorElement()
        : base(new TextBox())
 
    {
        this.StretchHorizontally = true;
        this.StretchVertically = true;
        ((TextBox)this.HostedControl).AcceptsReturn = true;
        ((TextBox)this.HostedControl).AcceptsTab = true;
        ((TextBox)this.HostedControl).Multiline = true;
        ((TextBox)this.HostedControl).KeyDown += new KeyEventHandler(MyTextBoxEditorElement_KeyDown);
    }
 
    void MyTextBoxEditorElement_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Escape ||
            (e.KeyCode == Keys.Enter && e.Modifiers == Keys.Shift))
        {
            RadGridView grid = (RadGridView)this.ElementTree.Control;
            grid.GridBehavior.ProcessKeyDown(new KeyEventArgs(e.KeyCode));
        }
    }
}

I hope this helps. If you need further assistance, don't hesitate to contact me back.

Sincerely yours,
Jack
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
GridView
Asked by
wima
Top achievements
Rank 1
Answers by
Jack
Telerik team
Share this question
or