dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7
  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,663
    What does "having problems getting it to work" mean?

    What code are you running?

    What errors are you getting?

  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 11:00 AM.
    Bob Rouse
    Dimension Data

  4. #4
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663

  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,663
    Thank you for coming back and posting your solution. It could help someone else with the same or similiar problem.

Similar Threads

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






×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.