New row on Enter key only?

6 posts, 0 answers
  1. Jill-Connie Lorentsen
    Jill-Connie Lorentsen avatar
    118 posts
    Member since:
    Nov 2008

    Posted 07 Mar 2012 Link to this post

    Is it possible for the user NOT to enter a new row when hitting the Arrow down- key? I want the new row to be added only when hitting Enter.

    Regards, Jill-Connie Lorentsen

  2. Svett
    Admin
    Svett avatar
    728 posts

    Posted 12 Mar 2012 Link to this post

    Hello Jill-Connie,

    You can create a custom edit manager where you can change the default behavior:

    public class CustomRadGridViewEditManager : GridViewEditManager
    {
        public CustomRadGridViewEditManager(RadGridViewElement gridView)
            : base(gridView)
        {
     
        }
     
        protected override void OnPositionChanging(PositionChangingEventArgs args)
        {
            GridViewRowInfo row = this.GridViewElement.Template.CurrentRow;
     
            if (row is GridViewNewRowInfo && args.Row != row)
            {
                this.CloseEditor();
                args.Cancel = true;
            }
     
            base.OnPositionChanging(args);
     
            args.Cancel = false;
        }
    }

    Then you should replace the default one in the following way:
    this.radGridView1.GridViewElement.EditorManager = new CustomRadGridViewEditManager(this.radGridView1.GridViewElement);

    I hope this helps.

    Greetings,
    Svett
    the Telerik team
    RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
  3. Jill-Connie Lorentsen
    Jill-Connie Lorentsen avatar
    118 posts
    Member since:
    Nov 2008

    Posted 13 Mar 2012 Link to this post

    Thanks for your answer, but it doesn't solve my problem completely. Maybe I didn't explain my scenario well enough.

    I have a grid with ComboBox-columns and TextBox-columns. The ComboBox-columns are filled with items from the database. A typical scenario when the user adds a new row is that he starts at the first column, and uses TAB to move to the next one. In a ComboBox column he uses the ArrowDown to chose one of the items, and then TAB to move on. In the Textbox columns he often presses ArrowDown by mistake, and then the new row is added (or, it won't be, as some values are missing).

    From what I see from what you suggest is that all the values that are entered are removed, and the user has to start all over again. I simply want the user to be able to go back to entering the rest of the values in the new row, and then adding it by pressing ENTER when done.

    I hope you understand what I mean.

  4. Svett
    Admin
    Svett avatar
    728 posts

    Posted 16 Mar 2012 Link to this post

    Hi Jill,

    In that case you should create a custom grid behavior which will prevent adding new row in the particular cases:

    public class RadGridBehavior : BaseGridBehavior
    {
        public override bool ProcessKeyDown(KeyEventArgs keys)
        {
            if (this.GridViewElement.IsInEditMode && keys.KeyCode != Keys.Enter && this.GridViewElement.CurrentRow is GridViewNewRowInfo)
            {
                return false;
            }
     
            return base.ProcessKeyDown(keys);
        }
    }

    Then you should replace the default one in the following way:
    this.radGridView1.GridBehavior = new RadGridBehavior();

    I hope this helps.

    Regards,
    Svett
    the Telerik team
    RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
  5. Amand
    Amand avatar
    25 posts
    Member since:
    Aug 2012

    Posted 23 Dec 2014 Link to this post

    I continue here, as my problem is close from this. I want to be able to add the new row only when hitting enter and to prevent adding the new row when clicking outside the new row.
    The first piece of code is OK for clicking on another line of the table. Using the the Leave event + CancelAddNewRow() on the new row is OK for clicking outside the grid view.
    private void ontGridViewLeave(object sender, EventArgs e)
    {
        // Do not add new row when clicking outside the grid view.
        var newRow = _mappingTableListGridView.TableElement.GetRowElement(_mappingTableListGridView.MasterView.TableAddNewRow) as GridNewRowElement;
        if (newRow != null)
        {
            ((GridViewNewRowInfo)newRow.RowInfo).CancelAddNewRow();
        }
    }


    However, I did not manage to deal with the case when the user clicks in the grid view but under the last row (in the blank space). How to prevent the new row to be added in this case ?

    Thanks,

    Amand.     
  6. Stefan
    Admin
    Stefan avatar
    2908 posts

    Posted 24 Dec 2014 Link to this post

    Hi Amand,

    Thank you for writing.

    Do handle this case, you should create a GridViewBehavior and override the OnMouseDown method:
    public class RadGridBehavior : BaseGridBehavior
    {
        public override bool OnMouseDown(MouseEventArgs e)
        {
            if (this.GridViewElement.IsInEditMode && this.GridViewElement.CurrentRow is GridViewNewRowInfo)
            {
                return false;
            }
     
            return base.OnMouseDown(e);
        }
    }

    Here is how to put this behavior in action:
    this.radGridView1.GridBehavior = new RadGridBehavior();

    Also, here is an easier way to get the GridViewNewRowInfo:
    void radGridView1_Leave(object sender, EventArgs e)
    {
        radGridView1.MasterView.TableAddNewRow.CancelAddNewRow();
    }

    I hope that you find this information useful. Should you have any other questions, do not hesitate to contact us.

    Regards,
    Stefan
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top