locked
Get all values from a property of IList object (as combobox do) RRS feed

  • Question

  • Hi, 

    I want to do the same as combobox do when assign datasource/displaymember properties: Get all values of field/property named as .DisplayMember from .Datasource object.

    In a combobox, when you assign an object (datatable, dataview, list(of ..), etc) to .Datasource, you can define 'field/property' to show in .DisplayMember). I want to do exactly the same.

    Datasource is defined as 'An object that implements the IList interface or an Array. '

    Then, I need to create a function that returns a list/array/whaever from Object and Displaymember

    ' I tried reflection, but i can obtain field/property DisplayMember
    Function GetAllValues(From As Object, DisplayMember As String) As ¿IList?
    
    Dim list As IList = CType(From, IList)
    
    list.ToList(Displaymember)
    
    Dim listReturn As New List()
    
    ' Obviusly, this doesnt work ;-)
    For Each oItem In list
       listReturn.Add oItem.Gettype().GetProperty(DisplayMember)
    Next oItem
    
    
    End Function

    Thanks

    Wednesday, July 17, 2019 6:16 AM

All replies

  • If the data are homogeneous, then try this function:

    Iterator Function GetAllValues(dataSource As Object, displayMember As String) As IEnumerable(Of Object)
    
       Dim lst = CType(dataSource, IList)
    
       If lst.Count = 0 Then Exit Function
    
       Dim type = lst(0).GetType
       Dim prop = type.GetProperty(displayMember)
    
       For Each item In lst
    
          Dim value = prop.GetValue(item)
          Yield value
    
       Next
    
    End Function
    

    If you need a list, then apply '.ToList' to obtained result. 

    Wednesday, July 17, 2019 8:30 AM
  • Hi,

    Thanks for your answer, but doesnt works for all objects as I asked, works only for IList:

    "In a combobox, when you assign an object (datatable, dataview, list(of ..), etc) to .Datasource, you can define 'field/property' to show in .DisplayMember). I want to do exactly the same."

    For example, when datasource is a datatable, your function doesnt works. An example in a Form with 2 listboxs:

    Public Class Form1
        Dim dt As New DataTable()
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            For n = 1 To 3
                dt.Columns.Add("col" & n.ToString, GetType(Integer))
            Next
    
            For m = 1 To 15
                dt.Rows.Add(m, m * 10, m + 10)
            Next
    
            ListBox1.DisplayMember = "col2"
            ListBox1.DataSource = dt
    
            Try
                Dim lst = GetAllValues(dt, "col2").ToList
            Catch ex As Exception
                Debug.Assert(False)
            End Try
        End Sub
    
        Iterator Function GetAllValues(dataSource As Object, displayMember As String) As IEnumerable(Of Object)
    
            Dim lst = TryCast(dataSource, IList)
    
            ' What I can DO when DataSource is not a IList???
            If lst Is Nothing Then
                Dim ilstsource = TryCast(dataSource, System.ComponentModel.IListSource)
                Debug.Assert(False)
            Else
                If lst.Count = 0 Then Exit Function
    
                Dim type = lst(0).GetType
                Dim prop = type.GetProperty(displayMember)
    
                For Each item In lst
                    Dim value = prop.GetValue(item)
                    Yield value
                Next
            End If
        End Function
    End Class

    When DataSource is DataTable, how i can obtain AllValues from DisplayMember? Combo & Listbox do ;-)

    Thanks again for your interest on answer my question.

    PD: Maybe 'source code' from ComboBox helps ;-)


    • Edited by LG DES Wednesday, July 17, 2019 9:38 AM
    Wednesday, July 17, 2019 9:27 AM
  • If you want to deal with DataTable too, not with IList and arrays only, then check this:

    Iterator Function GetAllValues(dataSource As Object, displayMember As String) As IEnumerable(Of Object)
    
        Dim lst = TryCast(dataSource, IList)
        If lst IsNot Nothing Then
    
            If lst.Count = 0 Then Exit Function
    
            Dim type = lst(0).GetType
            Dim prop = type.GetProperty(displayMember)
    
            For Each item In lst
    
                Dim value = prop.GetValue(item)
                Yield value
    
            Next
    
        Else
    
            Dim dt = TryCast(dataSource, DataTable)
            If dt IsNot Nothing Then
    
                For Each row As DataRow In dt.Rows
                    Dim value = row(displayMember)
                    Yield value
                Next
    
            End If
    
        End If
    
    End Function
    


    • Edited by Viorel_MVP Wednesday, July 17, 2019 10:46 AM
    Wednesday, July 17, 2019 10:45 AM
  • I want deal with every object that can be assigned to .DataSource in a combobox. DataTable is an example, dataview can be another example, etc.

    I would a 'generic' function that makes same function as combobox/listbox do with DataSource/DisplayMember combination.

    Thanks

    Wednesday, July 17, 2019 11:49 AM
  • Hi,

    Imports System.ComponentModel
    
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim mydt As New DataTable
            mydt.Columns.Add("column1")
            mydt.Columns.Add("column2")
            mydt.Columns.Add("column3")
            mydt.Rows.Add(New Object() {1, 2, 3})
            mydt.Rows.Add(New Object() {4, 5, 6})
            mydt.Rows.Add(New Object() {7, 8, 9})
            Dim mc As New MyClass1(mydt)
            DataGridView1.DataSource = mc.Datasource
        End Sub
        Public Class MyClass1
            Dim MyDatasource As IListSource
    
            Public Sub New(dt As DataTable)
                MyDatasource = dt
            End Sub
    
            Public ReadOnly Property Datasource() As IListSource
                Get
                    Return MyDatasource
                End Get
            End Property
        End Class
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, July 25, 2019 6:59 AM
  • Hi,

    That dont answer my question.

    How get all 'DisplayMember' values from Datasource (object)?

    DataSource: every object that can be assigned to a combobox/listbox.DataSource property.

    Values: Property/Field/whatever named 'DisplayMember' from every item in DataSource object

    Question is not for an 'specific' object (datatable/list of()/etc.) Question is about obtain values from every object can be assigned to combobox/listbox.Datasource.

    Thxs

    Tuesday, July 30, 2019 6:47 AM