We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

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

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#

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; 
    } 
} 
 
 
Please check the sample and let us know if you need further assistance on this. 
 
Regards, 
Susmitha S 


Loader.
Live Chat Icon For mobile
Up arrow icon