Skip to main content
A newer version of this page is available. .

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.v21.1.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();
        }
    } 
    
See Also