Skip to content

Dhanaguvi/how-to-maintain-the-scroll-bar-value-when-itemssource-changed-in-wpf-datagrid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 

Repository files navigation

How to maintain the scroll bar value when ItemsSource changed in WPF DataGrid (SfDataGrid)

How to maintain the scroll bar value when ItemsSource changed in WPF DataGrid (SfDataGrid)?

About the sample

By default SfDataGrid cannot maintain the scrollbar position when ItemsSource changed. But you can achieve this by get and set the scrollbar position using SfDataGrid.ItemsSourceChanged event.

this.dataGrid.ItemsSourceChanged += DataGrid_ItemsSourceChanged;

private void DataGrid_ItemsSourceChanged(object sender, GridItemsSourceChangedEventArgs e)
{
    if (columnName.Count > 0)
    {
        foreach (var col in columnName)
        {
            this.dataGrid.GroupColumnDescriptions.Add(new GroupColumnDescription() { ColumnName = col });
        }
        foreach (Group group in dataGrid.View.Groups)
        {
            var isExpandGroup = group;
            var key = expandedGroups.FirstOrDefault(colu => colu.Key.ToString() == isExpandGroup.Key.ToString());
            do
            {
                if (key != null)
                    dataGrid.ExpandGroup(isExpandGroup);

                if (isExpandGroup.Groups != null)
                {
                    isExpandGroup = isExpandGroup.Groups[0];
                    key = expandedGroups.FirstOrDefault(col => col.Groups[0].Key.ToString() == group.Groups[0].Key.ToString());
                }
                else
                    isExpandGroup = null;
            } while (isExpandGroup != null);
        }
    }
    VisualContainer container = this.dataGrid.GetVisualContainer();
    container.ScrollRows.ScrollBar.Value = this.Scrollbarvalue;
    container.InvalidateMeasureInfo();
}

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    var groups = dataGrid.View.Groups;
    foreach (Group group in groups)
    {
        if (group.IsExpanded)
            expandedGroups.Add(group);
    }
    foreach (GroupColumnDescription groupColumnDescriptions in dataGrid.GroupColumnDescriptions)
        columnName.Add(groupColumnDescriptions.ColumnName);
    VisualContainer container = this.dataGrid.GetVisualContainer();
    double scrollValue = container.ScrollRows.ScrollBar.Value;
    this.Scrollbarvalue = scrollValue;
    //change Items source
    this.dataGrid.ItemsSource = viewModel.Ordersnew;
}

Requirements to run the demo

Visual Studio 2015 and above versions

About

How to maintain the scroll bar value when ItemsSource changed in WPF DataGrid (SfDataGrid)?

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages