473,503 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding/removing rows from bound custom collections

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
Nov 20 '05 #1
0 2984

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1235
by: CGuy | last post by:
Hi, I'm working on an ASPX page which has a DataGrid. This DataGrid is bound to a custom object. My requirement is that at rune time, I want the datagrid to display the items that match a...
0
3111
by: Dave Elliott | last post by:
After inserting a new data row to a DataTable that is bound to a datagrid, I am unable to change data in a row that is after the newly added row without getting bizarre results. I have added the...
11
2269
by: Steven D'Aprano | last post by:
Suppose I create a class with some methods: py> class C: .... def spam(self, x): .... print "spam " * x .... def ham(self, x): .... print "ham * %s" % x .......
4
5448
by: DotNetJunky | last post by:
I have built a control that runs an on-line help system. Depending on the category you selected via dropdownlist, it goes out and gets the child subcategories, and if there are any, adds a new...
0
3477
by: Dave | last post by:
Tried posting in the Winform Forum without much luck, so posting here... After inserting a new data row to a DataTable that is bound to a datagrid, I am unable to change data in a row that is...
3
4855
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
12
1583
by: Bob Jones | last post by:
I have an odd business requirement and I think that the implementation is not correct in the terms of OOP development. Any help on the concepts would be very appreciated! We currently have a...
4
9618
by: =?Utf-8?B?VGVycnk=?= | last post by:
I have a DataGridView bound to a collection of Custom Objects. I have enabled adding new rows, but since my object does not have a public constructor (it uses a factory method), I get an error...
3
9667
by: jehugaleahsa | last post by:
Hello: I am binding a DataGridView with a BindingList<T>, where T is a custom business object that implements INotifyPropertyChanged. When you bind a DataGridView to a DataTable, it has this...
0
7199
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7076
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7453
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5576
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4670
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3162
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3151
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1507
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
377
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.