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

Automatically add new row

8 Answers 762 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Scott R
Top achievements
Rank 1
Scott R asked on 24 Jun 2009, 08:30 PM
I would like to have the grid automatically add a new row when the user presses "Tab" from the last cell in the last row of the grid.

This works if the last row of the grid is an "existing" row (meaning the row was present when the grid DataSource was set). However, it does not work if the user is already entering a "new" row.

For example:

1. Grid is displayed with 2 existing rows.
2. Focus is given to the top-left cell in the grid.
3. User presses "Tab" to navigate across the first row (editing data as needed).
4. User presses "Tab" while in the last column of the first row and focus moves to first column of 2nd row (this is good).
5. User presses "Tab" to navigate across the 2nd row (editing data as needed).
6. User presses "Tab" while in the last column of the 2nd row and focus moves to the first column of a "new" 3rd row (this is good).
7. User presses "Tab" to navigate across the "new" 3rd row (editing data as needed).
8. User presses "Tab" while in the last column of the "new" 3rd row - but nothing happens.
9. User gets confused, can't figure out how to add more rows (this is bad).
10. User calls me (this is really, really bad!!)

Any ideas on how to make the grid keep adding more rows automatically?

8 Answers, 1 is accepted

Sort by
0
Accepted
Victor
Telerik team
answered on 26 Jun 2009, 06:36 PM
Hi Scott R,

Here is an example on how to achieve the behavior you are after:
 
public partial class Form1 : Form  
{  
    public Form1()  
    {  
        InitializeComponent();  
        this.radGridView1.GridBehavior = new MyBehavior();  
    }  
}  
 
public class MyBehavior : BaseGridBehavior  
{  
    protected override bool ProcessTabKey(KeyEventArgs keys)  
    {  
        if (this.GridControl.CurrentCell.ColumnIndex == this.GridControl.Columns.Count - 1 && this.GridControl.CurrentCell.RowIndex == this.GridControl.Rows.Count - 1)  
        {  
            this.GridControl.Rows.AddNew();  
            return true;  
        }  
 
        return base.ProcessTabKey(keys);  
    }  
 
Basically you need to inherit BaseGridBehavior and override the ProcessTabKey method. There you should implement the necessary logic to go to a new line after the last cell. From here you will need to implement validation logic to prevent the user from going to the next row without entering correct or any information.
 
Write back if you need further assistance.

All the best,
Victor
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Scott R
Top achievements
Rank 1
answered on 30 Jun 2009, 09:46 PM
That almost got it, but it left the focus in the last column of the new row which is still confusing. I figured out how to move the focus to the first row:

GridViewDataRowInfo row = this.GridControl.Rows.AddNew();   
row.Cells["Product"].BeginEdit(); 

 

 

Now it works great.
Thanks.

 

0
Mohan
Top achievements
Rank 1
answered on 27 Jul 2009, 02:22 AM
for the same kind of module can you please give me a example in VB.NET, if you can attach a sample code that will be much appreciated.

Thank you

Raj
0
Victor
Telerik team
answered on 27 Jul 2009, 08:40 AM
Hello Mohan,

Please use this code converter and the provided code. There is nothing more, you just need to add some rows to the grid.

Kind regards,
Victor
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
ol
Top achievements
Rank 1
answered on 18 Aug 2010, 04:49 PM
Hi.

I have this problem working with a hierarchy gridview, but the solution doesn't work because the new row is added tho the parent gridview not the child template, and the tab key moves to next parent row, not the new row in the current group. Trying to add the row to the child template doesn't work.

Is there another way to do this.

Thanks
0
Jack
Telerik team
answered on 24 Aug 2010, 07:47 AM
Hi ol,

You could use the code below in case of a hierarchy:

this.GridControl.CurrentView.ViewInfo.ViewTemplate.Rows.AddNew();

Please let me know whether this helps. If not, please open a new ticket and send me your application and I will be glad to assist you further.

I am looking forward to your reply.

Greetings,
Jack
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
xjgs_xhj@163.com
Top achievements
Rank 1
answered on 17 Sep 2010, 10:11 AM

 Public Class MyBehavior
        Inherits BaseGridBehavior
        Protected Overrides Function ProcessTabKey(ByVal keys As KeyEventArgs) As Boolean
            If Me.GridControl.CurrentCell.ColumnIndex = Me.GridControl.Columns.Count - 1 AndAlso Me.GridControl.CurrentCell.RowIndex = Me.GridControl.Rows.Count - 1 Then
                Me.GridControl.Rows.AddNew()
                Return True
            End If

            Return MyBase.ProcessTabKey(keys)
        End Function
    End Class



I try this VB.NET code.It raise an error 'ProcessTabKey is not the member of Telerik.WinControls.UI.BaseGridBehavior'.
0
Jack
Telerik team
answered on 22 Sep 2010, 02:04 PM
Hi,

In our latest release we changed slightly the BaseGridBehavior class. You should use the ProcessKey method instead and check the arguments to determine whether the pressed key is Tab. Please consider the following code snippet:
Public Overrides Function ProcessKey(ByVal keys As System.Windows.Forms.KeyEventArgs) As Boolean
 
    If keys.KeyCode = System.Windows.Forms.Keys.Tab Then
        If Me.GridControl.CurrentCell.ColumnIndex = Me.GridControl.Columns.Count - 1 AndAlso Me.GridControl.CurrentCell.RowIndex = Me.GridControl.Rows.Count - 1 Then
            Me.GridControl.Rows.AddNew()
            Return True
        End If
    End If
 
    Return MyBase.ProcessKey(keys)
End Function

I hope this helps.

Kind regards, Jack
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
Scott R
Top achievements
Rank 1
Answers by
Victor
Telerik team
Scott R
Top achievements
Rank 1
Mohan
Top achievements
Rank 1
ol
Top achievements
Rank 1
Jack
Telerik team
xjgs_xhj@163.com
Top achievements
Rank 1
Share this question
or