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

New row on Enter key only?

5 Answers 338 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jill-Connie Lorentsen
Top achievements
Rank 1
Jill-Connie Lorentsen asked on 07 Mar 2012, 02:13 PM

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

5 Answers, 1 is accepted

Sort by
0
Svett
Telerik team
answered on 12 Mar 2012, 02:14 PM
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 >>
0
Jill-Connie Lorentsen
Top achievements
Rank 1
answered on 13 Mar 2012, 10:19 AM

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.

0
Svett
Telerik team
answered on 16 Mar 2012, 12:03 PM
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 >>
0
Amand
Top achievements
Rank 1
answered on 23 Dec 2014, 02:35 PM
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.     
0
Stefan
Telerik team
answered on 24 Dec 2014, 01:13 PM
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.

 
Tags
GridView
Asked by
Jill-Connie Lorentsen
Top achievements
Rank 1
Answers by
Svett
Telerik team
Jill-Connie Lorentsen
Top achievements
Rank 1
Amand
Top achievements
Rank 1
Stefan
Telerik team
Share this question
or