Skip to main content

DataControlBase.RestoreStateOnSourceChange Property

Gets or sets whether to retain the control’s select, focus, check, and group states when a new ItemsSource is assigned.

Namespace: DevExpress.Xpf.Grid

Assembly: DevExpress.Xpf.Grid.v23.2.Core.dll

NuGet Package: DevExpress.Wpf.Grid.Core

Declaration

public bool RestoreStateOnSourceChange { get; set; }

Property Value

Type Description
Boolean

true, to retain the control states; otherwise, false.

Remarks

Use the DataControlBase.RestoreStateKeyFieldName property to specify a key field that is used to compare two objects when the state is restored.

<dxg:GridControl Name="grid" ItemsSource="{Binding Items}" AutoGenerateColumns="AddNew" SelectionMode="Row" 
                 RestoreStateOnSourceChange="True" RestoreStateKeyFieldName="ID">
    <dxg:GridControl.View>
        <dxg:TableView />
    </dxg:GridControl.View>
</dxg:GridControl>
public class ViewModel : ViewModelBase {
    public ObservableCollection<Item> Items {
        get { return GetValue<ObservableCollection<Item>>(nameof(Items)); }
        private set { SetValue(value, nameof(Items)); }
    }
}
public class Item : BindableBase {
    public string Name {
        get { return GetValue<string>(nameof(Name)); }
        set { SetValue(value, nameof(Name)); }
    }
    public int ID { 
        get { return GetValue<int>(nameof(ID)); }
        set { SetValue(value, nameof(ID)); }
    }      
    public int GroupID {
        get { return GetValue<int>(nameof(GroupID)); }
        set { SetValue(value, nameof(GroupID)); }
    }
} 

Requirements

  • The current and new sources should have the same column sets (the same properties).

  • If the DataControlBase.RestoreStateKeyFieldName property is not specified, the GridControl uses the Object.Equals method to compare objects. You should override this method. If you use the TreeListView, override the Object.GetHashCode method additionally.

    public class Item : BindableBase {
        public string Name {
            get { return GetValue<string>(nameof(Name)); }
            set { SetValue(value, nameof(Name)); }
        }
        public int ID { 
            get { return GetValue<int>(nameof(ID)); }
            set { SetValue(value, nameof(ID)); }
        }      
        public int GroupID {
            get { return GetValue<int>(nameof(GroupID)); }
            set { SetValue(value, nameof(GroupID)); }
        }
        public override bool Equals(object obj) {
            Item item = obj as Item;
            if(item == null) return false;
            return item.ID == ID;
        }
        public override int GetHashCode() {
            return ID.GetHashCode();
        }
    } 
    

The following code snippets (auto-collected from DevExpress Examples) contain references to the RestoreStateOnSourceChange property.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

See Also