New to Telerik UI for WinForms? Download free 30-day trial

Handling Editors' events

In some cases you may need to perform a specific operation depending on the user input in the editor while the editor is still opened.

For example if you are in GridViewTextBoxColumn, the editor for the cells in this column is RadTextBoxEditor. You may need to set specific text in the text box editor when the user presses CTRL + L. In this case, you should subscribe to the KeyDown event of the RadTextBoxEditorElement in the CellEditorInitialized event handler. The editors in RadGridView are reused, so we define a field which prevents us from subscribing to the KeyDown more than once.

public HandlingEditorsEvents()
{
    InitializeComponent();
    radGridView1.CellEditorInitialized += new Telerik.WinControls.UI.GridViewCellEventHandler(radGridView1_CellEditorInitialized);
}
bool tbSubscribed = false;
void radGridView1_CellEditorInitialized(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
{
    RadTextBoxEditor tbEditor = this.radGridView1.ActiveEditor as RadTextBoxEditor;
    if (tbEditor != null)
    {
        if (!tbSubscribed)
        {
            tbSubscribed = true;
            RadTextBoxEditorElement tbElement = (RadTextBoxEditorElement)tbEditor.EditorElement;
            tbElement.KeyDown += new KeyEventHandler(tbElement_KeyDown);
        }
    }
}
void tbElement_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Control)
    {
        if (e.KeyCode == Keys.L)
        {
            ((RadTextBoxEditorElement)sender).Text = "Default text";
        }
    }
}