Skip to main content

DataControlBase.RestoreStateKeyFieldName Property

Gets or sets a name of key field that is used to compare two objects when the state is restored.

Namespace: DevExpress.Xpf.Grid

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

NuGet Package: DevExpress.Wpf.Grid.Core

Declaration

public string RestoreStateKeyFieldName { get; set; }

Property Value

Type Default Description
String ""

A key field name.

Remarks

Set the DataControlBase.RestoreStateOnSourceChange property to true to retain the control’s select, focus, check, and group states when a new ItemsSource is assigned.

<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 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 RestoreStateKeyFieldName 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