DataGrid remove item error bind with own class inherit from collection 
Author Message
 DataGrid remove item error bind with own class inherit from collection

Hi

With Visual Basic .NET and CollectionBase inherits Own Class

1. I want to
I try to remove item from a class bound to a dataGrid.

2. The problems:
The DataGrid seems to refresh with old data and it cause run time error

3. Error from VB.NET
An unhandled exception of type 'System.IndexOutOfRangeException' occurred in
system.windows.forms.dll
Additional information: No value at index 2.

I know, I delete those data !!!

4. The source code

4.1 Form Class
Private B as Books

Add button :
B.Add(New Book("Title-1", "Autor-1"))
B.Add(New Book("Title-2", "Autor-2"))
B.Add(New Book("Title-3", "Autor-3"))
DataGrid1.DataSource = B
Remove Button :
B.RemoveAt(0)

4.2 Class
Imports System
Imports System.Collections
Public Class Books
Inherits CollectionBase
Default Public Property Item(ByVal Index As Integer) As Book
Get
Return CType(List.Item(Index), Book)
End Get
Set(ByVal Value As Book)
List.Item(Index) = Value
End Set
End Property
Public Function Add(ByVal Item As Book) As Integer
Return List.Add(Item)
End Function

Public Sub Remove(ByVal Item As Book)
List.Remove(Item)
End Sub
End Class
Public Class Book
Private FTitle As String
Private FAuthor As String
Public Sub New(ByVal title As String, ByVal author As String)
FAuthor = author
FTitle = title
End Sub
Public Property Author() As String
Get
Return FAuthor
End Get
Set(ByVal Value As String)
FAuthor = Value
End Set
End Property
Public Property Title() As String
Get
Return FTitle
End Get
Set(ByVal Value As String)
FTitle = Value
End Set
End Property
End Class

Thanks a lot



Wed, 25 May 2005 00:08:59 GMT  
 DataGrid remove item error bind with own class inherit from collection
I am having the same problem.  Did you find a work around?


Quote:
> Hi

> With Visual Basic .NET and CollectionBase inherits Own Class

> 1. I want to
> I try to remove item from a class bound to a dataGrid.

> 2. The problems:
> The DataGrid seems to refresh with old data and it cause run time error

> 3. Error from VB.NET
> An unhandled exception of type 'System.IndexOutOfRangeException' occurred
in
> system.windows.forms.dll
> Additional information: No value at index 2.

> I know, I delete those data !!!

> 4. The source code

> 4.1 Form Class
> Private B as Books

> Add button :
> B.Add(New Book("Title-1", "Autor-1"))
> B.Add(New Book("Title-2", "Autor-2"))
> B.Add(New Book("Title-3", "Autor-3"))
> DataGrid1.DataSource = B
> Remove Button :
> B.RemoveAt(0)

> 4.2 Class
> Imports System
> Imports System.Collections
> Public Class Books
> Inherits CollectionBase
> Default Public Property Item(ByVal Index As Integer) As Book
> Get
> Return CType(List.Item(Index), Book)
> End Get
> Set(ByVal Value As Book)
> List.Item(Index) = Value
> End Set
> End Property
> Public Function Add(ByVal Item As Book) As Integer
> Return List.Add(Item)
> End Function

> Public Sub Remove(ByVal Item As Book)
> List.Remove(Item)
> End Sub
> End Class
> Public Class Book
> Private FTitle As String
> Private FAuthor As String
> Public Sub New(ByVal title As String, ByVal author As String)
> FAuthor = author
> FTitle = title
> End Sub
> Public Property Author() As String
> Get
> Return FAuthor
> End Get
> Set(ByVal Value As String)
> FAuthor = Value
> End Set
> End Property
> Public Property Title() As String
> Get
> Return FTitle
> End Get
> Set(ByVal Value As String)
> FTitle = Value
> End Set
> End Property
> End Class

> Thanks a lot



Wed, 25 May 2005 06:46:31 GMT  
 DataGrid remove item error bind with own class inherit from collection
Actually nevermind I found a work around:
http://www.CSharpfriends.com/Members/Main/Tutorials/get_tutorial.aspx...
81

Or the simplier method is just to set the datasource to nothing and then set
it back to the object before it paints again.

...previous code here
DataGrid1.DataSource = B
Remove Button :
B.RemoveAt(0)
DataGrid1.DataSource=Nothing
DataGrid1.DataSource=B
'You may want to repaint it now but that part is optional
DataGrid1.Update()


Quote:
> Hi

> With Visual Basic .NET and CollectionBase inherits Own Class

> 1. I want to
> I try to remove item from a class bound to a dataGrid.

> 2. The problems:
> The DataGrid seems to refresh with old data and it cause run time error

> 3. Error from VB.NET
> An unhandled exception of type 'System.IndexOutOfRangeException' occurred
in
> system.windows.forms.dll
> Additional information: No value at index 2.

> I know, I delete those data !!!

> 4. The source code

> 4.1 Form Class
> Private B as Books

> Add button :
> B.Add(New Book("Title-1", "Autor-1"))
> B.Add(New Book("Title-2", "Autor-2"))
> B.Add(New Book("Title-3", "Autor-3"))
> DataGrid1.DataSource = B
> Remove Button :
> B.RemoveAt(0)

> 4.2 Class
> Imports System
> Imports System.Collections
> Public Class Books
> Inherits CollectionBase
> Default Public Property Item(ByVal Index As Integer) As Book
> Get
> Return CType(List.Item(Index), Book)
> End Get
> Set(ByVal Value As Book)
> List.Item(Index) = Value
> End Set
> End Property
> Public Function Add(ByVal Item As Book) As Integer
> Return List.Add(Item)
> End Function

> Public Sub Remove(ByVal Item As Book)
> List.Remove(Item)
> End Sub
> End Class
> Public Class Book
> Private FTitle As String
> Private FAuthor As String
> Public Sub New(ByVal title As String, ByVal author As String)
> FAuthor = author
> FTitle = title
> End Sub
> Public Property Author() As String
> Get
> Return FAuthor
> End Get
> Set(ByVal Value As String)
> FAuthor = Value
> End Set
> End Property
> Public Property Title() As String
> Get
> Return FTitle
> End Get
> Set(ByVal Value As String)
> FTitle = Value
> End Set
> End Property
> End Class

> Thanks a lot



Wed, 25 May 2005 06:56:57 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. DataGrid remove item error bind with own class inherit from collection

2. DataGrid binding to a custom collection class

3. own collection (inherits CollectionBase)

4. ItemRemove event don't fire when removing last item in items collection object

5. Returning a collection class (inherited from CollectionBase) via webService

6. How to inherit a collection class with no Public Sub New

7. My own Collection Class in Access97

8. Removing and item from a custom collection

9. Help removing items from collection,read issue.

10. Removing items from a collection

11. Problems removing items from collection by index

12. Removing a collection item problems ?

 

 
Powered by phpBB® Forum Software