Force New Row on End Edit

5 posts, 1 answers
  1. Matthew
    Matthew avatar
    16 posts
    Member since:
    Oct 2010

    Posted 20 Feb 2012 Link to this post

    Hi there,

    I have a grid which is used for data entry and uses the New Row button.
    I have set MasterTemplate.AddNewBoundRowBeforeEdit set to true so that the data is ready to be saved to the database on every cellEndEdit.

    It looks like the new row still is not added to the data source until you leave that row by pressing up or down.

    I need the new row added to the data source on the very first CellEndEdit. I need this because i have other controls on the form which react to rows being added.

    I can use a SendKeys hack to achieve the desired result:

    protected void CellEndEdit(object sender, GridViewCellEventArgs e)
    {
        Save(e.Row.DataBoundItem);
     
        if (e.Row is GridViewNewRowInfo)
        {
            Debug.WriteLine("Attempting refresh");
            SendKeys.Send("{UP}");
        }
    }


    with this code i can edit a cell, press tab, and i end up on the next cell and the new row button appears below the current row.

    Do you know of a better way to produce the same result ?
    I've tried GridNavigator.SelectPreviousRow(1) in both CellEndEdit and CellValueChanged and it does nothing.

    Thanks,
    Matt
  2. Jack
    Admin
    Jack avatar
    2333 posts

    Posted 23 Feb 2012 Link to this post

    Hello Matt,

    I am not sure that I understand your scenario. Please, try the following. Bind RadGridView to a DataTable and handle its TableNewRow event. Run your project and try to add a new row, you will see that this event fires at the moment when you click on new row button. Please note that the new row will be added in RadGridView.Rows collection when it is validated. If you want to change this behavior, you should create a custom grid behavior. Here is a sample:
    this.radGridView1.GridBehavior = new CustomGridBehavior();
     
    public class CustomGridBehavior : BaseGridBehavior
    {
        public override bool OnMouseDown(MouseEventArgs e)
        {
            GridDataCellElement cellElement = GridControl.ElementTree.GetElementAtPoint(e.Location) as GridDataCellElement;
            if (cellElement != null && cellElement.RowElement is GridNewRowElement)
            {
                GridViewColumn column = cellElement.ColumnInfo;
                GridViewRowInfo row = this.GridControl.Rows.AddNew();
                row.IsCurrent = true;
                column.IsCurrent = true;
                GridControl.BeginEdit();
                return true;
            }
            return base.OnMouseDown(e);
        }
    }

    I hope this helps.

    All the best, Jack
    the Telerik team
    RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
  3. Matthew
    Matthew avatar
    16 posts
    Member since:
    Oct 2010

    Posted 23 Feb 2012 Link to this post

    Hi Jack,

    Thanks for the reply.

    Your sample code does not create the desired effect. the app needs to support data entry via keyboard only. basically, I need to support the following use case:
    1. user presses down arrow to move down to the new row.
    2. user edits the cell and hits tab/enter to trigger end edit
    3. The current row becomes a regular data row and the new row button appears below the current row.

    This way there is always a blank new row available. Is this possible ?

    Thanks again,
    Matt
  4. Answer
    Jack
    Admin
    Jack avatar
    2333 posts

    Posted 24 Feb 2012 Link to this post

    Hi Matt,

    Yes, this is possible. However, this is a custom behavior and it can be implemented by overriding the BaseGridBehavior like demonstrated in my previous post. Here is a sample:
    public class CustomGridBehavior : BaseGridBehavior
    {
        public override bool ProcessKey(KeyEventArgs keys)
        {
            if (keys.KeyCode == Keys.Tab || keys.KeyCode == Keys.Enter)
            {
                if (GridControl.CurrentRow is GridViewNewRowInfo && GridControl.IsInEditMode)
                {
                    GridControl.EndEdit();
                    GridControl.Rows[GridControl.Rows.Count - 1].IsCurrent = true;
                    GridControl.GridNavigator.SelectNextColumn();
                    GridControl.BeginEdit();
                    return true;
                }
            }
            return base.ProcessKey(keys);
        }

    I hope it helps.
     
    Kind regards,
    Jack
    the Telerik team
    RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
  5. Matthew
    Matthew avatar
    16 posts
    Member since:
    Oct 2010

    Posted 27 Feb 2012 Link to this post

    Excellent!

    thank you for pointing me in the right direction.

    Cheers,
    Matt
Back to Top