Cell KeyDown Event

9 posts, 0 answers
  1. Minkyu Lee
    Minkyu Lee avatar
    6 posts
    Member since:
    Apr 2010

    Posted 29 Jun 2010 Link to this post

    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.
  2. Thomas
    Thomas avatar
    28 posts
    Member since:
    Jun 2010

    Posted 29 Jun 2010 Link to this post

    Hi,

    you can use the CurrentCell property?!
  3. Martin Vasilev
    Admin
    Martin Vasilev avatar
    1061 posts

    Posted 02 Jul 2010 Link to this post

    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
  4. Andrew L
    Andrew L avatar
    7 posts
    Member since:
    Apr 2010

    Posted 06 Aug 2010 Link to this post

    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
  5. Martin Vasilev
    Admin
    Martin Vasilev avatar
    1061 posts

    Posted 11 Aug 2010 Link to this post

    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
  6. Yogish
    Yogish avatar
    12 posts
    Member since:
    Dec 2010

    Posted 22 Dec 2010 Link to this post

    thank u. its really a gud example
  7. reyhane
    reyhane avatar
    7 posts
    Member since:
    Oct 2019

    Posted 16 Dec 2019 in reply to Martin Vasilev Link to this post

    How to set KeyDown event for GridViewBrowseColumn ?(I set it editable With the help of this link)
  8. Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3635 posts

    Posted 16 Dec 2019 Link to this post

    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.
  9. reyhane
    reyhane avatar
    7 posts
    Member since:
    Oct 2019

    Posted 16 Dec 2019 in reply to Dess | Tech Support Engineer, Sr. Link to this post

    Thank you very much
Back to Top