strongly-typed collection as a datasource


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: strongly-typed collection as a datasource

  1. #1
    Join Date
    Jan 2004
    Location
    Alexandria, VA
    Posts
    392

    strongly-typed collection as a datasource

    I am trying to use a strongly-typed collection as a datasource for a listbox (and a datagridview) and having problems getting it to work. I am using VB.NET 2008.

    Can anyone point me to some good examples of this so I can try to figure out what I'm doing wrong?

    Thanks...
    Bob Rouse
    Dimension Data

  2. #2
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    What does "having problems getting it to work" mean?

    What code are you running?

    What errors are you getting?
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  3. #3
    Join Date
    Jan 2004
    Location
    Alexandria, VA
    Posts
    392
    Here is an example of the class/collection:

    Code:
    Public Class CBarCode
        Private nBarCodeNumber As Integer = 0
        Private sBarCodeValue As String = ""
        Private sBarCodeType As String = ""
        Private nBarCodePage As Integer = 0
        Private sIndexField As String = ""
    End Class
    
    Public Class CBarCodes
        Inherits Dictionary(Of String, CBarCode)
    End Class
    I tried binding the collection directly, but got an error: "Complex DataBinding accepts as a data source either an IList or an IListSource". So I added a "BindingSource" object and used it.

    Here is an example of the code:
    Code:
    lstBarCodes.BeginUpdate()
    lstBarCodes.DataSource = CBarCodesBindingSource
    CBarCodesBindingSource.DataSource = oBarCodes
    'CBarCodesBindingSource.DataMember = "IDCSTProcess.CBarCode"
    lstBarCodes.DisplayMember = "BarCodeValue"
    'lstBarCodes.ValueMember = "BarCodeNumber"
    lstBarCodes.EndUpdate()
    lstBarCodes.Refresh()
    With this code, I get one value in the list: [0, CBarCode]

    If I uncomment the "DataMember" line, I get this error: "DataMember property 'CBarCode' cannot be found on the DataSource". If I uncomment the ValueMember line, I get "cannot bind to new value member. Parameter name: value".

    It seems like it is trying to bind to the collection itself, rather than the members of the collection. This is where I think I am going wrong, and would like to see some examples of it being done.
    Last edited by brouse; 01-08-2009 at 12:00 PM.
    Bob Rouse
    Dimension Data

  4. #4
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  5. #5
    Join Date
    Jan 2004
    Location
    Alexandria, VA
    Posts
    392
    so it seems I need to implement the IList interface?

    I will investigate further (and try some things) and report back with my results.

    Thanks for the links.
    Bob Rouse
    Dimension Data

  6. #6
    Join Date
    Jan 2004
    Location
    Alexandria, VA
    Posts
    392
    So the IList interface definitely is the key. I didn't want to lose my Dictionary collection features (key lookup), especially since I only needed the IList for the DataSourcing/Displaying. So what I did was add a method to the collection class that returns a List representation of the objects:

    Code:
    Public Function ToIList() As IList(Of CBarCode)
    
            Dim oList As New List(Of CBarCode)
            Dim oBarCode As CBarCode
    
            For Each oBarCode In MyBase.Values
                oList.Add(oBarCode)
            Next
    
            Return oList
    
    End Function
    Then I just changed my form code to use the List:

    Code:
    Private oList As New List(Of IDCSTProcess.CBarCode)
    
                oList = oBarCodes.ToIList()
                lstBarCodes.DataSource = oList
                lstBarCodes.DisplayMember = "BarCodeValue"
                lstBarCodes.ValueMember = "BarCodeNumber"
    Thanks for pointing me in the right direction...
    Bob Rouse
    Dimension Data

  7. #7
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Thank you for coming back and posting your solution. It could help someone else with the same or similiar problem.
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

Similar Threads

  1. strongly typed collection classes - error
    By rachelricketts in forum .NET
    Replies: 3
    Last Post: 02-10-2006, 11:34 AM
  2. collection philosophy
    By will in forum .NET
    Replies: 3
    Last Post: 10-26-2002, 08:40 PM
  3. Replies: 7
    Last Post: 06-24-2002, 08:32 PM
  4. Replies: 5
    Last Post: 06-06-2002, 07:05 PM
  5. Strong Typed Collection Classes
    By Marcel Gnoth in forum .NET
    Replies: 2
    Last Post: 06-09-2001, 11:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center