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

Cell KeyDown Event

8 Answers 890 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Minkyu Lee
Top achievements
Rank 1
Minkyu Lee asked on 29 Jun 2010, 09:21 AM
Hi,

When I press 'F1' in a cell, I want to show a messagebox that include radGridView1 information. (ex, rows.count, columns.count etc.)

private void ShowMessage()
{
    RadMessageBox.Show(radGridView1.Rows.Count);
    RadMessageBox.Show(radGridView1.Columns.Count);
}

I know that a radGridView has KeyDown event, but I can't find KeyDown event of cells.

Please help me.

Thank you.

8 Answers, 1 is accepted

Sort by
0
Thomas
Top achievements
Rank 1
answered on 29 Jun 2010, 09:33 AM
Hi,

you can use the CurrentCell property?!
0
Martin Vasilev
Telerik team
answered on 02 Jul 2010, 01:19 PM
Hi Minkyu,

Thank you for writing.

The KeyDown event is not fired if the RadGridView is in edit mode. If that is the case in your scenario, you can subscribe to editor's KeyDown event to achieve desired requirement. Please consider the following code: 
public Form1()
{
    InitializeComponent();
    this.radGridView1.KeyDown += new KeyEventHandler(radGridView1_KeyDown);
    this.radGridView1.CellBeginEdit += new GridViewCellCancelEventHandler(radGridView1_CellBeginEdit);
    this.radGridView1.CellEndEdit += new GridViewCellEventHandler(radGridView1_CellEndEdit);
}
void radGridView1_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
{
    BaseGridEditor editor = this.radGridView1.ActiveEditor as BaseGridEditor;
    if (editor != null)
    {
        RadItem editorElement = editor.EditorElement as RadItem;
        editorElement.KeyDown += new KeyEventHandler(radGridView1_KeyDown);
    }
}
void radGridView1_CellEndEdit(object sender, GridViewCellEventArgs e)
{
    BaseGridEditor editor = this.radGridView1.ActiveEditor as BaseGridEditor;
    if (editor != null)
    {
        RadItem editorElement = editor.EditorElement as RadItem;
        editorElement.KeyDown -= radGridView1_KeyDown;
    }
}
void radGridView1_KeyDown(object sender, KeyEventArgs e)
{
    MessageBox.Show(String.Format("Grid's RowsCount: {0}", this.radGridView1.RowCount));
}

I hope this helps. Let me know if you have any additional questions.

Greetings,
Martin Vasilev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Andrew L
Top achievements
Rank 1
answered on 06 Aug 2010, 02:34 PM
Hi,

In my project gridView.ActivEditor in CellEndEdit is always null. I can't remove Event from editor, and that event work for all editors activate in my gridView. How can I remove that event ? This is my sample :
public partial class Form1 : RadForm
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void gridView_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
        {
            BaseGridEditor editor = this.gridView.ActiveEditor as BaseGridEditor;
            if (editor != null)
            {
                ((RadTextBoxEditorElement)editor.EditorElement).KeyDown += new KeyEventHandler(cellEditor_KeyDown);
            }
        }
 
        void cellEditor_KeyDown(object sender, KeyEventArgs e)
        {
            RadTextBoxEditorElement editor = sender as RadTextBoxEditorElement;
            switch (e.KeyData)
            {
                //some code
                default:
                    editor.Text += "@";
                    break;
            }
        }
 
        private void gridView_CellEndEdit(object sender, GridViewCellEventArgs e)
        {
            BaseGridEditor editor = this.gridView.ActiveEditor as BaseGridEditor;
 
            if (editor != null)
            {
                ((RadTextBoxEditorElement)editor.EditorElement).KeyDown -= new KeyEventHandler(cellEditor_KeyDown);
            }
        }
    }

Telerik WinControls 2010.2.10.713
0
Martin Vasilev
Telerik team
answered on 11 Aug 2010, 07:31 PM
Hello Andrew L,

Thank you for contacting us.

The life-cycle of the editor in the new RadGridView has been slightly changed and now it is true that the ActiveEditor property returns null in the CellEndEdit event. You can overcome this by saving the reference of the editor in a local variable. Here is a sample snippet which illustrates this approach:

BaseGridEditor _gridEditor;
void radGridView1_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
{
    _gridEditor = this.radGridView1.ActiveEditor as BaseGridEditor;
    if (_gridEditor != null)
    {
        RadItem editorElement = _gridEditor.EditorElement as RadItem;
        editorElement.KeyDown += new KeyEventHandler(radGridView1_KeyDown);
    }
}
void radGridView1_CellEndEdit(object sender, GridViewCellEventArgs e)
{
    if (_gridEditor != null)
    {
        RadItem editorElement = _gridEditor.EditorElement as RadItem;
        editorElement.KeyDown -= radGridView1_KeyDown;
    }
    _gridEditor = null;
}

I hope you find this helpful.

Best wishes,
Martin Vasilev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Yogish
Top achievements
Rank 1
answered on 22 Dec 2010, 11:16 AM
thank u. its really a gud example
0
reyhane
Top achievements
Rank 1
answered on 16 Dec 2019, 09:13 AM
How to set KeyDown event for GridViewBrowseColumn ?(I set it editable With the help of this link)
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 16 Dec 2019, 01:32 PM
Hello, Reyhane,     

Note that while the RadGridView is not in edit mode, it handles mouse and keyboard input by a GridRowBehavior. Depending on the row type, RadGridView introduces different behaviors. The following help article demonstrates how to use them: https://docs.telerik.com/devtools/winforms/controls/gridview/rows/row-behaviors.html 

As to the keyboard handling  for a GridViewBrowseColumn when RadGridView is in edit mode, please refer to the following code snippet: 
        private void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
        {
            GridBrowseEditor browseEditor = e.ActiveEditor as GridBrowseEditor;
            if (browseEditor != null)
            {
                RadBrowseEditorElement element = browseEditor.EditorElement as RadBrowseEditorElement;
                element.TextBoxItem.KeyDown -= TextBoxItem_KeyDown;
                element.TextBoxItem.KeyDown += TextBoxItem_KeyDown;
            }
        }

        private void TextBoxItem_KeyDown(object sender, KeyEventArgs e)
        {
            Console.WriteLine(e.KeyData);
        }
I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
reyhane
Top achievements
Rank 1
answered on 17 Dec 2019, 05:15 AM
Thank you very much
Tags
GridView
Asked by
Minkyu Lee
Top achievements
Rank 1
Answers by
Thomas
Top achievements
Rank 1
Martin Vasilev
Telerik team
Andrew L
Top achievements
Rank 1
Yogish
Top achievements
Rank 1
reyhane
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or