How to move the SelectedItem to the new inserted record
Hi,
I have a SfDatagrid and I insert a new record in my grid using the event "AddNewRowInitiating"
Everything is ok.
But when the users presses the Return key, the newly created record is appended to the grid (at the last row), but the "SelectedItem" becomes the first record (row) of the datagrid and not the last as I would like.
I would like, in fact, that after the editing is finished, the focus (SelectedItem) is put on the just added record (so the last row).
Is there any way to obtain this?
Is there any event that I can join to in order to set SelectedItem property to the new inserted record ?
Thank you in advance.
Silvio
SIGN IN To post a reply.
3 Replies
AS
Ambarish Srinivasan
Syncfusion Team
January 19, 2016 09:02 AM UTC
Hi Silvio,
You can move selection to newly added row by changing the SelectedItem in RowValidated event.
C#
Sample:
Sample
Please let us know if you have any questions.
Regards,
Ambarish.
You can move selection to newly added row by changing the SelectedItem in RowValidated event.
C#
|
sfdatagrid.RowValidated+=sfdatagrid_RowValidated;
void sfdatagrid_RowValidated(object sender, RowValidatedEventArgs args) { if(sfdatagrid.IsAddNewIndex(args.RowIndex)) { sfdatagrid.Dispatcher.BeginInvoke(new Action(() => { sfdatagrid.SelectedItem = args.RowData; sfdatagrid.ScrollInView(sfdatagrid.SelectionController.CurrentCellManager.CurrentRowColumnIndex); })); } |
Sample:
Sample
Please let us know if you have any questions.
Regards,
Ambarish.
KD
Kim Dobranski
March 11, 2020 02:59 AM UTC
How do I accomplish this in winforms?
SS
Susmitha Sundar
Syncfusion Team
March 11, 2020 06:28 PM UTC
Hi Kim,
Thank you for your update.
You can achieve your requirement of “Scroll and select the newly added row” by using the CurrentCellActivated event and Records.CollectionChnaged event. Please refer to the below code,
|
sfDataGrid.CurrentCellActivating += sfDataGrid_CurrentCellActivating;
sfDataGrid.View.Records.CollectionChanged += Records_CollectionChanged;
void Records_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
{
// To scroll the grid to the newly added row position.
var scrollValue = this.sfDataGrid.TableControl.ScrollRows.Distances.GetCumulatedDistanceAt(e.NewStartingIndex);
this.sfDataGrid.TableControl.ScrollRows.ScrollInView(e.NewStartingIndex);
this.sfDataGrid.TableControl.VerticalScroll.Value = (int)scrollValue;
sfDataGrid.TableControl.BeginInvoke(new Action(() =>
{
this.sfDataGrid.SelectedItem = e.NewItems[0];
}));
}
}
void sfDataGrid_CurrentCellActivating(object sender, Syncfusion.WinForms.DataGrid.Events.CurrentCellActivatingEventArgs e)
{
if (sfDataGrid.IsAddNewRowIndex(sfDataGrid.CurrentCell.RowIndex) && sfDataGrid.View.IsAddingNew && e.DataRow.RowIndex == sfDataGrid.GetAddNewRowIndex() + 1)
{
e.Cancel = true;
}
} |
Sample link: https://www.syncfusion.com/downloads/support/forum/121708/ze/AddNewRow_Scroll-1286831846
Please check the sample and let us know if you need further assistance on this.
Regards,
Susmitha S
SIGN IN To post a reply.
- 3 Replies
- 4 Participants
-
SI Silvio
- Jan 18, 2016 04:26 PM UTC
- Mar 11, 2020 06:28 PM UTC