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

[Solved] Insert row between existing rows

6 Answers 256 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Edward
Top achievements
Rank 1
Edward asked on 17 Nov 2010, 12:52 PM
Hello,

Is there a way to allow insertion of a new row between existing grid rows? Our customer is used to Excel, where (of course) you can right-click on any row and select Insert Row.

Thanks a lot

Ed

6 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 17 Nov 2010, 12:56 PM
Hello,

 You can use Insert() method for the collection bound to the grid and the grid will be updated properly after such collection changed event. 

Greetings,
Vlad
the Telerik team
See What's New in RadControls for Silverlight in Q3 2010 on Tuesday, November 16, 2010 11:00 AM - 12:00 PM EST or 10:00 PM - 11:00 PM EST: Register here>>
0
Edward
Top achievements
Rank 1
answered on 17 Nov 2010, 01:19 PM
Thank you. So I guess a complete solution would be to set up a context menu on the grid containing an Insert Row item, which when selected detects which row was clicked on and inserts into the underlying collection at the appropriate position. Does that sound a fairly straightforward job?

Ed
0
Vlad
Telerik team
answered on 17 Nov 2010, 01:23 PM
Hello,

 Indeed this will work in your case. You can check this demo for more info about the row context menu. 

Regards,
Vlad
the Telerik team
See What's New in RadControls for Silverlight in Q3 2010 on Tuesday, November 16, 2010 11:00 AM - 12:00 PM EST or 10:00 PM - 11:00 PM EST: Register here>>
0
Edward
Top achievements
Rank 1
answered on 22 Nov 2010, 11:44 AM
I've implemented Insert Row as follows:

// Create new row for the grid, in a generic way (so that this code can be used in any grid)
object newRow = gridView.Items.AddNew();
// The new row is now at the bottom of the grid, so immediately remove it
gridView.Items.Remove(newRow);
 
// Insert the row at the current position
// gridView.Items.Insert throws an exception at this point, so insert directly into the underlying collection
int currentIndex = gridView.Items.IndexOf(row);
((IList)gridView.ItemsSource).Insert(currentIndex, newRow);
 
// Select the new row
gridView.SelectedItem = newRow;
gridView.Items.MoveCurrentTo(newRow);


This works, except that while the new row is correctly selected, the currently-selected cell does not change (it is still on the previous row). How can I programmatically move the selected cell up one row?

Thanks very much

Ed
0
Mahmut
Top achievements
Rank 2
answered on 23 Nov 2010, 09:56 PM
Hello,
i think you can try this (instead of MoveCurrentTo):
gridView.CurrentItem = gridView.SelectedItem;


mahmut
0
Edward
Top achievements
Rank 1
answered on 24 Nov 2010, 11:43 AM
Nearly :) The inserted row is selected and the current row indicator points at the new row, but the selected cell border disappears. No matter - this will do us just fine!

Thanks very much to Telerik for all their fast and useful help on these forums.

Ed
Tags
GridView
Asked by
Edward
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Edward
Top achievements
Rank 1
Mahmut
Top achievements
Rank 2
Share this question
or