Add new row and set focus to second cell on gridview keyboard focus

3 posts, 1 answers
  1. Marcel
    Marcel avatar
    41 posts
    Member since:
    Jan 2014

    Posted 21 Mar Link to this post

    Hello,

    I have the following requirements:

    • When the user enters from outside the grid into the empty grid a new row should be created and the second cell should be selected in edit mode.
    • When the user enters from outside the grid into a grid with existing rows, the second cell should be selected in edit mode.

    I am using the enter-key to move to the next field in a particular viewmodel, instead of the tab-key.

    How can I accomplish this, I tried searching a solution but couldnt find anything that accomplish this when entering the grid from outside?

     

    Any help would be greatly appreciated.

    Marcel

  2. Answer
    Dilyan Traykov
    Admin
    Dilyan Traykov avatar
    371 posts

    Posted 24 Mar Link to this post

    Hello Marcel,

    I will try to address both scenarios you have described.

    First of all, you should check if the respective GridView has any items.

    1) If the GridView is empty, you can call RadGridView's BeginInsert() method and then set the CurrentCell to the cell in the second column:

    if (this.RadGridView.Items.Count == 0)
    {
        this.RadGridView.BeginInsert();
        this.RadGridView.CurrentCellInfo = new GridViewCellInfo(RadGridView.CurrentItem, RadGridView.Columns[1]);
    }

    2) If there are items in the GridView, you can select the second cell of the last item, for example, and call RadGridView's BeginEdit() method:

    else
    {
        RadGridView.Items.MoveCurrentToLast();
        this.RadGridView.CurrentCellInfo = new GridViewCellInfo(RadGridView.CurrentItem, RadGridView.Columns[1]);
        this.RadGridView.BeginEdit();
    }

    It is up to you to pick the event on which you will trigger the respective operation. If you're navigating with the Enter key, for example, you can handle the window's PreviewKeyUp event like so:

    public MainWindow()
    {
        InitializeComponent();
        this.PreviewKeyUp += MainWindow_PreviewKeyUp;
    }
     
    void MainWindow_PreviewKeyUp(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter && FocusManager.GetFocusedElement(this) == this.RadGridView)
        {
            HandleNavigation();
        }
    }
     
    void HandleNavigation()
    {
        if (this.RadGridView.Items.Count == 0)
        {
            this.RadGridView.BeginInsert();
            this.RadGridView.CurrentCellInfo = new GridViewCellInfo(RadGridView.CurrentItem, RadGridView.Columns[1]);
        }
        else
        {
            RadGridView.Items.MoveCurrentToLast();
            this.RadGridView.CurrentCellInfo = new GridViewCellInfo(RadGridView.CurrentItem, RadGridView.Columns[1]);
     
            if (this.RadGridView.CurrentCell.IsInEditMode)
            {
                this.RadGridView.CommitEdit();
            }
     
            this.RadGridView.BeginEdit();
        }
    }

    I'm attaching a sample project to better illustrate this. Please let me know if this approach would work for you.

    Regards,
    Dilyan Traykov
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  3. UI for WPF is Visual Studio 2017 Ready
  4. Marcel
    Marcel avatar
    41 posts
    Member since:
    Jan 2014

    Posted 22 Apr in reply to Dilyan Traykov Link to this post

    Hello Dilyan,

    Thanks for the support, had to alter the example code a little bit but now its working!

    Regards,

    Marcel

     

     

Back to Top