Best way to create a ListView in C# (or

Posted on 2004-11-22
Last Modified: 2009-07-29
I am rewriting an app in C# that was written in VB6.  I want to write a common routine where I can pass in parameters, including an arraylist of data, and have the listview created for me in that common routine, passed back, and then displayed on a form.  The purpose is to not have code that creates a listivew in the 100+ forms that we will have.  I have tried passing back a listview control into a procedure called from the form, but it doesn't see to work.  Any ideas?
Question by:sgildner
    LVL 15

    Expert Comment

    I think you want to already have the listview on the form and just pass a reference of it to the common function.
    LVL 5

    Accepted Solution

    This should be fairly straight forward:

    public static ListView CreateListView(arg1, arg2, arg3)
         ListView li = new ListView();
         //Business Logic to change list view items here
         //custom formatting here
         return li;

    In the place where you are getting the list view, you need to make sure to add it to the form.  

    HOWEVER, I would recommend against this...

    You are still going to have to have custom code to put the listview in the correct place, and I don't see why you would want to do that programmatically vs. in the designer.  

    It might be better to create a class that has the logic to add the items correctly, so you can always call this function.  Have all of the other forms inherit from this form, and assign the listview variable that is used in the superclass to the actual listview in the constructor.  

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    In one of my recent projects, I was working with IP cameras, I need to take some pictures from the camera and do some processing on it. The first step, was to retrieve the image from camera into Image object. So that it can be displayed or …
    Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    737 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

    22 Experts available now in Live!

    Get 1:1 Help Now