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

SfDataGrid - AutoScroll and AutoEdit

Hi,

I am trying to do the following:
  • I have a SfDataGrid bound to a ObservableCollection<>
  • When the user clicks a button outside of the SfDataGrid I append a new item at the end of the Collection
  • The SfDataGrid should scroll automaticaly to the last row (representing the added item)
  • The SfDataGrid should start EditMode for this row

I have got the Scrolling to work by using the DataContextChanged event and the CollectionChanged event - but this is a very complex and ugly Code for such a simple Problem.

Do you have a nice solution for this?


Thanks Kurt


4 Replies

SP Sowndaiyan Paulpandi Syncfusion Team October 6, 2016 12:51 PM UTC


Hi Kurt,

Thanks for contacting Syncfusion Support.

In SfDataGrid, you can achieve your requirement “AutoScroll and AutoEdit while adding new item in collection’ by using the ScrollToEnd method and BeginEdit method like the below code example,

C#
 
private void Button_Click(object sender, RoutedEventArgs e) 
        { 
            (this.DataContext as ViewModel).OrdersDetails.Add(new OrderInfo() { CustomerID = "100009", Freight = 100 }); 
 
            this.dataGrid.GetVisualContainer().ScrollOwner.ScrollToEnd(); 
 
            Dispatcher.Invoke(new Action(() => 
            { 
                var viewModel = this.dataGrid.DataContext as ViewModel; 
                //find the RowIndex for particular record 
                var RowIndex = this.dataGrid.ResolveToRowIndex(viewModel.OrdersDetails[viewModel.OrdersDetails.Count - 1]); 
                // CurrentCell is set if MappingName is EmployeeID 
                this.dataGrid.MoveCurrentCell(new RowColumnIndex(RowIndex, 0)); 
                this.dataGrid.SelectionController.CurrentCellManager.BeginEdit(); 
            }),System.Windows.Threading.DispatcherPriority.ApplicationIdle); 
          
        } 

We have prepared a sample as per your requirement and you can download the sample from the below location,

Sample : http://www.syncfusion.com/downloads/support/forum/126283/ze/WPF-1761942878
 

Regards,
Sowndaiyan
 



KZ Kurt Zimmermann October 7, 2016 12:57 PM UTC

Hi,

thank's very much. I implemented it the way you suggest and it works perfect. The only differnece, I put your code in the CollectionChanged event of the Collection:

model.Medications.CollectionChanged += (s, e1) =>

{

if (e1.Action == NotifyCollectionChangedAction.Add)

{

this.DataGrid.GetVisualContainer().ScrollOwner.ScrollToEnd();

...


Regards

Kurt


FP Farjana Parveen Ayubb Syncfusion Team October 10, 2016 12:13 PM UTC

Hi Kurt, 
 
Thank you for your response. 
 
Please let us know if you require further assistance from us. 
 
Regards, 
Farjana Parveen A 



RV Ruud van der Giessen replied to Sowndaiyan Paulpandi July 14, 2022 04:13 PM UTC

Hi,


Although it is a ticket from 2016 it was holding the answer about how I can automatically scroll to the end of the datagrid.

So it is very useful to have this information available!

Thanks!

Ruud



Loader.
Live Chat Icon For mobile
Up arrow icon