ListView column headers

Posted on 2011-05-10
Last Modified: 2012-06-27
Hi Experts,

If I have an arraylist populated with a customer class:


It is possible to bind a listview control to this arraylist so that the column header names are automatically taken from the class members so that the column headers would be:
firstname, lastName, age.
Question by:DColin
    LVL 2

    Expert Comment

    if your first three items in your arraylist are the headers then you can do something like

     Dim customerList() as your class
                ListView1.View = View.Details
                ListView1.Columns.Add(customerList(0), 50, HorizontalAlignment.Center)
                ListView1.Columns.Add(customerList(1), 50, HorizontalAlignment.Center)
                ListView1.Columns.Add(customerList(2), 20, HorizontalAlignment.Center)

    Open in new window


    Author Comment


    The header names are not part of the array.

    Is it possible to access the class members name. So for example:

    customer.firstName.membername would return "firstName"?
    LVL 83

    Expert Comment

    What are you trying to achieve? Make the display display any collection? Why not use a DataTable and a DataGridView?

    Author Comment


    Yes I am trying to create a display table that will display any class or data type and automatically set to headers to the name of the data variable.
    LVL 83

    Accepted Solution

    You can use Reflection to get all the properties of the class but then you would have to add columns to the listview before populating it as Listview does not create columns automatically (but the DataGridView does).

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    This video discusses moving either the default database or any database to a new volume.

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now