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