Version

UndeleteRecordsStrategy Class

Abstract class used to provide the ability to undelete records that were deleted through the DataPresenterBase control.
Syntax
'Declaration
 
Public MustInherit Class UndeleteRecordsStrategy 
public abstract class UndeleteRecordsStrategy 
Remarks

The DataPresenter controls provide the end-user with the ability to delete records through the user interface if the underlying data source supports deleting records. When undo functionality is enabled (see DataPresenterBase.IsUndoEnabled), you may allow the end user to undelete these records. To enable this capability, the Infragistics.Windows.DataPresenter.Events.RecordsDeletingEventArgs.UndeleteStrategy property must be set to a derived instance of a UndeleteRecordsStrategy that can perform the undeletion for the Infragistics.Windows.DataPresenter.Events.RecordsDeletingEventArgs.Records that are about to be deleted.

When an undo of deleted records is to be performed, the Undelete method is invoked. Derived classes implement this method and perform the undeletion either recreating new objects that have the same information as the deleted records or when possible reinserting the deleted records into the datasource. After the undeletion is performed, the ProcessUndeletedRecords method is invoked to allow you to set properties on the records such as the values of unbound cells.

If the records that were deleted had child records and the RestoreDescendantActions property returns true when the records are being deleted, the DataPresenter will attempt to reconnect any undo/redo actions associated with the descendants of the records that were deleted. If any actions were associated with the descendant records, the ProvideDescendantMapping method will be invoked to provide a means to map the old record information to the new dataitem. By default, this will only work if the underlying data items for the descendant records do not change.

Example
The following sample demonstrates how to provide a custom UndeleteStrategy to provide undo support for the deletion of records through the DataPresenter.

using Infragistics.Windows;
using Infragistics.Windows.DataPresenter;
using System.Collections;
using System.ComponentModel;
using System.Collections.Specialized;

	private void grid_RecordsDeleting(object sender, Infragistics.Windows.DataPresenter.Events.RecordsDeletingEventArgs e)
	{
		// during the RecordsDeleting the records have not been deleted. in order to 
		// allow the end user to recreate those records, you must provide a custom
		// undeletestrategy that can store the data needed before the delete and 
		// perform the undeletion when requested (when the user attempts to perform
		// an undo). this strategy is a basic one that will reinsert the original
		// item into the containing list so this would work with ibindinglist<t> and 
		// observablecollection<t> type classes. this will not work with datatable/
		// dataset. for more examples please refer to the advanced undo sample in 
		// the feature browser sample
		e.UndeleteStrategy = new ListUndeleteStrategy(e.Records);
	}

	private void grid_Initialized(object sender, EventArgs e)
	{
		DataPresenterBase dp = sender as DataPresenterBase;
		var list = new BindingList<Employee>();
		list.Add(new Employee( "John", new DateTime(2008, 1, 1) ));
		list.Add(new Employee( "Mary", new DateTime(2007, 8, 5) ));
		list.Add(new Employee( "Paul", new DateTime(2003, 3, 14) ));
		dp.DataSource = list;
	}

	#region ListUndeleteStrategy
	// This sample strategy assumes that the objects that were removed from the collection 
	// can be reinserted into the collection and therefore this does not work with things 
	// like dataview, datatable and dataset. you should refer to the advanced undo sample 
	// in the samples browser for more examples
	public class ListUndeleteStrategy : UndeleteRecordsStrategy
	{
		public ListUndeleteStrategy(IList<Record> records)
		{
		}

		public override bool CanUndelete(IList<UndeleteRecordsStrategy.RecordInfo> oldRecords)
		{
			return true;
		}

		public override IDictionary<RecordInfo, object> Undelete(IList<RecordInfo> oldRecords)
		{
			Dictionary<RecordInfo, object> oldToNewMapping = new Dictionary<RecordInfo, object>();

			RecordInfo[] records = oldRecords.ToArray();
			Comparison<RecordInfo> comparison = delegate(RecordInfo item1, RecordInfo item2)
			{
				return item1.DataItemIndex.CompareTo(item2.DataItemIndex);
			};

			// sort by the original index to ensure we get them in the original order
			Utilities.SortMergeGeneric(records, Utilities.CreateComparer(comparison));

			foreach (RecordInfo record in records)
			{
				RecordManager rm = record.RecordManager;
				IList list = rm.SourceItems as IList;

				if (list == null)
					continue;

				int newIndex = Math.Min(list.Count, record.DataItemIndex);
				object newDataItem = record.DataItem;

				list.Insert(newIndex, newDataItem);

				// since we're reusing the same object the old and new will be the same
				oldToNewMapping[record] = newDataItem;
			}

			return oldToNewMapping;
		}
	}
	#endregion //ListUndeleteStrategy

	#region Employee
	public class Employee
	{
		private string _name;
		private DateTime _hireDate;

		public Employee(string name, DateTime hireDate)
		{
			_name = name;
			_hireDate = hireDate;
		}

		public string Name
		{
			get { return _name; }
		}

		public DateTime HireDate
		{
			get { return _hireDate; }
		}
	} 
	#endregion //Employee
xmlns:igDP="http://infragistics.com/DataPresenter"

<igDP:XamDataGrid xmlns:igDP="http://infragistics.com/DataPresenter"
    
x:Name="grid" 
    
BindToSampleData="True"
    
IsUndoEnabled="True"
    
Initialized="grid_Initialized"
    
RecordsDeleting="grid_RecordsDeleting">
</igDP:XamDataGrid>
Imports Infragistics.Windows
Imports Infragistics.Windows.DataPresenter
Imports Infragistics.Windows.DataPresenter.UndeleteRecordsStrategy
Imports System.Globalization
Imports System.ComponentModel

    Private Sub grid_RecordsDeleting(ByVal sender As System.Object, ByVal e As Infragistics.Windows.DataPresenter.Events.RecordsDeletingEventArgs)
        ' during the RecordsDeleting the records have not been deleted. in order to 
        ' allow the end user to recreate those records, you must provide a custom
        ' undeletestrategy that can store the data needed before the delete and 
        ' perform the undeletion when requested (when the user attempts to perform
        ' an undo). this strategy is a basic one that will reinsert the original
        ' item into the containing list so this would work with ibindinglist<t> and 
        ' observablecollection<t> type classes. this will not work with datatable/
        ' dataset. for more examples please refer to the advanced undo sample in 
        ' the feature browser sample
        e.UndeleteStrategy = New ListUndeleteStrategy(e.Records)
    End Sub

    Private Sub grid_Initialized(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim dp As DataPresenterBase = DirectCast(sender, DataPresenterBase)
        Dim list As BindingList(Of Employee) = New BindingList(Of Employee)()
        list.Add(New Employee("John", New DateTime(2008, 1, 1)))
        list.Add(New Employee("Mary", New DateTime(2007, 8, 5)))
        list.Add(New Employee("Paul", New DateTime(2003, 3, 14)))
        dp.DataSource = list
    End Sub


#Region "ListUndeleteStrategy"
    ' This sample strategy assumes that the objects that were removed from the collection 
    ' can be reinserted into the collection and therefore this does not work with things 
    ' like dataview, datatable and dataset. you should refer to the advanced undo sample 
    ' in the samples browser for more examples
    Public Class ListUndeleteStrategy
        Inherits UndeleteRecordsStrategy
        Public Sub New(ByVal records As IList(Of Record))
        End Sub

        Public Overrides Function CanUndelete(ByVal oldRecords As IList(Of UndeleteRecordsStrategy.RecordInfo)) As Boolean
            Return True
        End Function

        Public Overrides Function Undelete(ByVal oldRecords As IList(Of RecordInfo)) As IDictionary(Of RecordInfo, Object)
            Dim oldToNewMapping As New Dictionary(Of RecordInfo, Object)
            Dim records As RecordInfo() = oldRecords.ToArray()

            Dim comparison As Comparison(Of RecordInfo) = Function(item1 As RecordInfo, item2 As RecordInfo) item1.DataItemIndex.CompareTo(item2.DataItemIndex)

            ' sort by the original index to ensure we get them in the original order
            Utilities.SortMergeGeneric(records, Utilities.CreateComparer(comparison))

            For Each record As RecordInfo In records
                Dim rm As RecordManager = record.RecordManager
                Dim list As IList = TryCast(rm.SourceItems, IList)

                If list Is Nothing Then Continue For

                Dim newIndex As Integer = Math.Min(list.Count, record.DataItemIndex)
                Dim newDataItem As Object = record.DataItem

                list.Insert(newIndex, newDataItem)

                ' since we're reusing the same object the old and new will be the same
                oldToNewMapping(record) = newDataItem
            Next record

            Return oldToNewMapping
        End Function
    End Class
#End Region 'ListUndeleteStrategy

#Region "Employee"
    Public Class Employee
        Private _name As String
        Private _hireDate As DateTime

        Public Sub New(ByVal name As String, ByVal hireDate As DateTime)
            _name = name
            _hireDate = hireDate
        End Sub

        Public ReadOnly Property Name() As String
            Get
                Return _name
            End Get
        End Property

        Public ReadOnly Property HireDate() As DateTime
            Get
                Return _hireDate
            End Get
        End Property
    End Class
#End Region 'Employee
Requirements

Target Platforms: Windows 10, Windows 8.1, Windows 8, Windows 7, Windows Server 2012, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also