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

How do you put the current cell into edit mode when the grid originally receives focus from a tab

1 Answer 312 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Andrew
Top achievements
Rank 1
Andrew asked on 05 Apr 2013, 11:42 AM
Let's say that a user tabs from another control to my RadGridView and it receives focus. I want the grid to set the currently selected cell into edit mode. So I tried the following:

void RadGridView1_GotFocus(object sender, System.Windows.RoutedEventArgs e)
{
    if (this.RadGridView1.CurrentCell != null)
    {
        if (!RadGridView1.CurrentCell.IsInEditMode)
        {
            RadGridView1.BeginEdit();
        }
    }
}

This works well, except when you try to tab off the last cell (row, column index). In that case, the grid receives focus for some reason and sets the last cell back into edit mode again. No matter how many times you try to tab off the last cell you cannot. This occurs when TabNavigation on the grid is set to Local.

In order to try and handle this issue I modified the GotFocus event like so:
void RadGridView1_GotFocus(object sender, System.Windows.RoutedEventArgs e)
{
    if (this.RadGridView1.CurrentCell != null)
    {
        var info = new GridViewCellInfo(RadGridView1.Items[RadGridView1.Items.Count - 1], RadGridView1.Columns[RadGridView1.Columns.Count - 1]);
 
        if (RadGridView1.CurrentCellInfo!= info && !RadGridView1.CurrentCell.IsInEditMode)
        {
            RadGridView1.BeginEdit();
        }
    }
}

Here I retrieve the GridViewCellInfo of the last cell. If the current cell is the last cell, I do nothing, ignoring the focus, and everything continues correctly. But if I cycle back through to the grid via tabbing later, and the grid receives focus, it won't put the last cell into edit mode, defeating the whole purpose in the process.

If you have any questions regarding what I've described please let me know. I can also supply a sample project if necessary.

Thanks.


1 Answer, 1 is accepted

Sort by
0
Accepted
Yoan
Telerik team
answered on 09 Apr 2013, 12:39 PM
Hi Andrew,

In order to achieve your goal, I can suggest you to modify the GotFocus event like so:

private void RadGridView1_GotFocus(object sender, RoutedEventArgs e)
       {
           if (this.RadGridView1.CurrentCell != null)
           {
                var focusedElement = FocusManager.GetFocusedElement();
 
               if (focusedElement == this.RadGridView1 && !RadGridView1.CurrentCell.IsInEditMode)
               {
                   RadGridView1.BeginEdit();
               }
           }
       }

I hope this is what you need.

Greetings,
Yoan
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
GridView
Asked by
Andrew
Top achievements
Rank 1
Answers by
Yoan
Telerik team
Share this question
or