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

Force New Row on End Edit

4 Answers 330 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Matthew
Top achievements
Rank 1
Matthew asked on 21 Feb 2012, 03:06 AM
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

4 Answers, 1 is accepted

Sort by
0
Jack
Telerik team
answered on 23 Feb 2012, 12:55 PM
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 >>
0
Matthew
Top achievements
Rank 1
answered on 24 Feb 2012, 04:01 AM
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
0
Accepted
Jack
Telerik team
answered on 24 Feb 2012, 04:15 PM
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 >>
0
Matthew
Top achievements
Rank 1
answered on 28 Feb 2012, 03:06 AM
Excellent!

thank you for pointing me in the right direction.

Cheers,
Matt
Tags
GridView
Asked by
Matthew
Top achievements
Rank 1
Answers by
Jack
Telerik team
Matthew
Top achievements
Rank 1
Share this question
or