Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3313914
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:10:15+00:00 2026-05-17T22:10:15+00:00

I have a ListView in virtual mode, and the underlying data is being stored

  • 0

I have a ListView in virtual mode, and the underlying data is being stored in a List<MyRowObject>. Each column of the ListView corresponds to a public string property of MyRowObject. The columns of my ListView are configurable during runtime, such that any of them can be disabled and they can be reordered. To return a ListViewItem for the RetrieveVirtualItem event, I have a method similar to:

class MyRowObject
{
    public string[] GetItems(List<PropertyInfo> properties)
    {
        string[] arr = new string[properties.Count];
        foreach(PropertyInfo property in properties)
        {
            arr[i] = (string)property.GetValue(this,null);
        }
        return arr;
    }
}

The event handler for RetrieveVirtualItem looks similar to:

private void listView_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
{
    e.Item = new ListViewItem(_virtualList[e.ItemIndex].GetItems(_currentColumns));
}

Maybe not surprisingly, benchmarking shows that this method is significantly slower than an implementation that accessed properties directly in a hardcoded order, and the slowdown is just significant enough that I would like to find a better solution.

The most promising idea I’ve had is to use an anonymous delegate to tell the MyRowObject class how to directly access the properties, but if it’s possible I couldn’t get the semantics right (given the name of a property stored in a string, is there a way I can write a closure to directly access that property?).

So, is there a nice way to avoid using reflection to populate my ListView without losing any functionality?

The open source extension of ListView is off limit because of company policy.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T22:10:15+00:00Added an answer on May 17, 2026 at 10:10 pm

    You could use these 2 functions

        private List<Func<T, string>> BuildItemGetters<T>(IEnumerable<PropertyInfo> properties)
        {
            List<Func<T, string>> getters = new List<Func<T, string>>();
            foreach (var prop in properties)
            {
                var paramExp = Expression.Parameter(typeof(T), "p");
    
                Expression propExp = Expression.Property(paramExp, prop);
                if (prop.PropertyType != typeof(string))
                    propExp = Expression.Call(propExp, toString);
    
                var lambdaExp = Expression.Lambda<Func<T, string>>(propExp, paramExp);
    
                getters.Add(lambdaExp.Compile());
            }
    
            return getters;
        }
    
        private string[] GetItems<T>(List<Func<T, string>> properties, T obj)
        {
            int count = properties.Count;
            string[] output = new string[count];
    
            for (int i = 0; i < count; i++)
                output[i] = properties[i](obj);
    
            return output;
        }
    

    Call the BuildItemGetters (sorry for the name, couldn’t think of anything 😉 once with a list of properties you want to get from the rows. Then just call the GetItems for each row. Where obj is the row and the list is the one you got from the other function.

    For T just use the class name of your Row, like:

    var props = BuildItemGetters<MyRowObject>(properties);
    string[] items = GetItems(props, row);
    

    ofcourse, only call the build when the columns change

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ListView on a page that displays a list of widgets. When
I have a ListView control that is in FullRowSelect mode, MultiSelect off and using
I have a listview that is binded to a ThreadSafeObservableCollection. The background of each
I'm using a ListView control in virtual and OwnerDraw mode to display a number
I have a List<ListViewItem> and I've used it in a ListView in VirtualMode There
I have a ListView control, and I'm trying to figure out the easiest/best way
I have a ListView which sometimes I need to put around 10000 items in.
I have a ListView in WPF that is databound to a basic table that
I have a ListView containing file names. These file names need to be draggable
I have a ListView inside of an Update Panel and wanted to change the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.