Hello. I have a custom collection that implements IBindingList (allownew and
allowremove are both true). I have bound it to a datagrid. I have add and
remove buttons on the screen. I want to implement those buttons, but I'm not
sure what the code should look like.
I tried what I thought was obvious code:
myCollection.AddNew()
and
myCollection.RemoveAt(myGrid.CurrentRowIndex)
but the grid doesn't update in either case. Clearly I'm missing something.
Can anyone give me an example of what the code for adding and removing
should look like?
I should point out that when I press delete with a row selected, the
collection updates, so the [ grid -> collection ] functionality appears to
be there. What I am after is [ collection -> grid ]. Is this possible and
if so, can anyone give me an example of what the code under the add and
remove buttons needs to look like?
In case it helps, here is the code for my class and collection:
Imports System.ComponentModel
Imports System.Collections
Public Class Employees
Inherits CollectionBase
Implements IBindingList
Protected _isSorted As Boolean = False
Protected _allowEdit As Boolean = True
Protected _allowRemove As Boolean = True
Protected _allowNew As Boolean = True
Protected _supportsSorting As Boolean = False
Protected _supportsSearching As Boolean = False
Protected _supportsChangeNotification As Boolean = True
Protected _listSortDirection As ListSortDirection
Public Event ListChanged(ByVal sender As Object, ByVal e As
ListChangedEventArgs) Implements IBindingList.ListChanged
Private resetEvent As New ListChangedEventArgs(ListChangedType.Reset, -1)
Private onListChanged1 As ListChangedEventHandler
Protected Overridable Sub RaiseListChanged(ByVal ev As
ListChangedEventArgs)
If Not (onListChanged1 Is Nothing) Then
onListChanged1(Me, ev)
End If
End Sub
Public Overridable ReadOnly Property AllowEdit() As Boolean Implements
IBindingList.AllowEdit
Get
Return _allowEdit
End Get
End Property
Public Overridable ReadOnly Property SortDirection() As ListSortDirection
Implements IBindingList.SortDirection
Get
Throw New NotSupportedException
End Get
End Property
Public Overridable ReadOnly Property AllowRemove() As Boolean Implements
IBindingList.AllowRemove
Get
Return _allowRemove
End Get
End Property
Public Overridable Function Find(ByVal [property] As PropertyDescriptor,
ByVal key As Object) As Integer Implements IBindingList.Find
Throw New NotSupportedException
End Function
Public Overridable Sub AddIndex(ByVal [property] As PropertyDescriptor)
Implements IBindingList.AddIndex
Throw New NotSupportedException
End Sub
Public Overridable ReadOnly Property AllowNew() As Boolean Implements
IBindingList.AllowNew
Get
Return _allowNew
End Get
End Property
Public Overridable Sub ApplySort(ByVal [property] As PropertyDescriptor,
ByVal direction As ListSortDirection) Implements IBindingList.ApplySort
Throw New NotSupportedException
End Sub
Public Overridable Sub RemoveSort() Implements IBindingList.RemoveSort
Throw New NotSupportedException
End Sub
Public Overridable ReadOnly Property SupportsSorting() As Boolean
Implements IBindingList.SupportsSorting
Get
Return _supportsSorting
End Get
End Property
Public Overridable ReadOnly Property SupportsSearching() As Boolean
Implements IBindingList.SupportsSearching
Get
Return _supportsSearching
End Get
End Property
Public Overridable ReadOnly Property IsSorted() As Boolean Implements
IBindingList.IsSorted
Get
Return _isSorted
End Get
End Property
Public Overridable Function AddNew() As Object Implements
IBindingList.AddNew
Dim employee As New Employee
Me.Add(employee)
Return employee
End Function
Public Overridable Sub RemoveIndex(ByVal [property] As PropertyDescriptor)
Implements IBindingList.RemoveIndex
Throw New NotSupportedException
End Sub
Public Overridable ReadOnly Property SupportsChangeNotification() As
Boolean Implements IBindingList.SupportsChangeNotification
Get
Return _supportsChangeNotification
End Get
End Property
Public Overridable ReadOnly Property SortProperty() As PropertyDescriptor
Implements IBindingList.SortProperty
Get
Throw New NotSupportedException
End Get
End Property
Protected Overrides Sub OnClear()
'
End Sub
Protected Overrides Sub OnInsert(ByVal index As Integer, ByVal value As
Object)
RaiseListChanged(New ListChangedEventArgs(ListChangedType.ItemAdded,
index))
End Sub
Protected Overrides Sub OnRemove(ByVal index As Integer, ByVal value As
Object)
RaiseListChanged(New ListChangedEventArgs(ListChangedType.ItemDeleted,
index))
End Sub
Protected Overrides Sub OnSet(ByVal index As Integer, ByVal oldValue As
Object, ByVal newValue As Object)
If Not oldValue Is newValue Then
RaiseListChanged(New ListChangedEventArgs(ListChangedType.ItemAdded,
index))
End If
End Sub
Public Sub New()
Me.List.Add(New Employee("Joe", "Schmo"))
Me.List.Add(New Employee("John", "Doe"))
Me.List.Add(New Employee("Bill", "Smith"))
Me.List.Add(New Employee("Dudley", "DoRight"))
End Sub
Default Public Property Item(ByVal index As Integer) As Employee
Get
Return CType(Me.List.Item(index), Employee)
End Get
Set(ByVal Value As Employee)
Me.List.Item(index) = Value
End Set
End Property
Public Sub Remove(ByVal employee As Employee)
Me.List.Remove(employee)
End Sub
Public Shadows Sub RemoveAt(ByVal index As Integer)
Me.List.RemoveAt(index)
End Sub
Public Function Add(ByVal employee As Employee) As Integer
Return Me.List.Add(employee)
End Function
End Class
Public Class Employee
Private mFirst As String
Private mLast As String
'Events name must be exactly "PropertyName" and Changed
Public Event FirstNameChanged As EventHandler
Public Event LastNameChanged As EventHandler
Public Sub New()
'
End Sub
Public Sub New(ByVal first As String, ByVal last As String)
mFirst = first
mLast = last
End Sub
Public Property FirstName() As String
Get
Return mFirst
End Get
Set(ByVal Value As String)
mFirst = Value
RaiseEvent FirstNameChanged(Me, EventArgs.Empty)
End Set
End Property
Public Property LastName() As String
Get
Return mLast
End Get
Set(ByVal Value As String)
mLast = Value
RaiseEvent LastNameChanged(Me, EventArgs.Empty)
End Set
End Property
End Class