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

  • Home
  • SEARCH
  • 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 3785486
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:32:09+00:00 2026-05-19T11:32:09+00:00

I have a .NET class I’d like to show in a DataGridView, and the

  • 0

I have a .NET class I’d like to show in a DataGridView, and the default databinding – setting the DGV’s DataSource to the object – produces 90% of my requirements (i.e. it’s outputting the public properties correctly and I can add sorting easily).

However, one of the properties I need to bind is a List which contains data which needs to be in separate columns after the other databound items. I’m stuck on how best to implement this.

My class looks something like this:

public class BookDetails
{
    public string Title { get; set; }
    public int TotalRating { get; set; }
    public int Occurrence { get; set; }
    public List<int> Rating { get; set; }
}

Ideally, I’d be able to expand that Rating property into a number of numeric columns to give an output like this at runtime:

Title | Total Rating | Occurrence | R1 | R2 | R3 … RN

It would also be useful to have Total Rating be calculated as the sum of all the individual ratings, but I’m updating that manually at the moment without issue.

  • 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-19T11:32:10+00:00Added an answer on May 19, 2026 at 11:32 am

    Like this?

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Windows.Forms;
    
    public class BookDetails
    {
        public string Title { get; set; }
        public int TotalRating { get; set; }
        public int Occurrence { get; set; }
        public List<int> Rating { get; set; }
    }
    
    class BookList : List<BookDetails>, ITypedList
    {
    
        public PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors)
        {
            var origProps = TypeDescriptor.GetProperties(typeof(BookDetails));
            List<PropertyDescriptor> newProps = new List<PropertyDescriptor>(origProps.Count);
            PropertyDescriptor doThisLast = null;
            foreach (PropertyDescriptor prop in origProps)
            {
    
                if (prop.Name == "Rating") doThisLast = prop;
                else newProps.Add(prop);
            }
            if (doThisLast != null)
            {
                var max = (from book in this
                           let rating = book.Rating
                           where rating != null
                           select (int?)rating.Count).Max() ?? 0;
                if (max > 0)
                {
                    // want it nullable to account for jagged arrays
                    Type propType = typeof(int?); // could also figure this out from List<T> in
                                                  // the general case, but make it nullable
                    for (int i = 0; i < max; i++)
                    {
                        newProps.Add(new ListItemDescriptor(doThisLast, i, propType));
                    }
                }
            }
            return new PropertyDescriptorCollection(newProps.ToArray());
        }
    
        public string GetListName(PropertyDescriptor[] listAccessors)
        {
            return "";
        }
    }
    
    class ListItemDescriptor : PropertyDescriptor
    {
        private static readonly Attribute[] nix = new Attribute[0];
        private readonly PropertyDescriptor tail;
        private readonly Type type;
        private readonly int index;
        public ListItemDescriptor(PropertyDescriptor tail, int index, Type type) : base(tail.Name + "[" + index + "]", nix)
        {
            this.tail = tail;
            this.type = type;
            this.index = index;
        }
        public override object GetValue(object component)
        {
            IList list = tail.GetValue(component) as IList;
            return (list == null || list.Count <= index) ? null : list[index];
        }
        public override Type PropertyType
        {
            get { return type; }
        }
        public override bool IsReadOnly
        {
            get { return true; }
        }
        public override void SetValue(object component, object value)
        {
            throw new NotSupportedException();
        }
        public override void ResetValue(object component)
        {
            throw new NotSupportedException();
        }
        public override bool CanResetValue(object component)
        {
            return false;
        }
        public override Type ComponentType
        {
            get { return tail.ComponentType; }
        }
        public override bool ShouldSerializeValue(object component)
        {
            return false;
        }
    }
    
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            var data = new BookList {
                new BookDetails { Title = "abc", TotalRating = 3, Occurrence = 2, Rating = new List<int> {1,2,1}},
                new BookDetails { Title = "def", TotalRating = 3, Occurrence = 2, Rating = null },
                new BookDetails { Title = "ghi", TotalRating = 3, Occurrence = 2, Rating = new List<int> {3, 2}},
                new BookDetails { Title = "jkl", TotalRating = 3, Occurrence = 2, Rating = new List<int>()},
            };
            Application.Run(new Form
            {
                Controls = {
                    new DataGridView {
                        Dock = DockStyle.Fill,
                        DataSource = data
                    }
                }
            });
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an assembly (.NET class library) that contains an object that implements EAP
Say I have a .NET class like so: public class Person { public string
I have created an ASP.NET class. In that class i would like to use
I have a .net class which makes a HTTP Request to a controller in
I have a .NET class library containing a class with a method that performs
If I have a .Net class that is not part of any namespace then
How can I invoke overloaded generic method in IronRuby? I have a .NET class
I have a MustInherit .NET class which declares a constructor with an integer parameter.
I have the following .Net class: public class Product { public int ID {get;set;}
I have an ASP.NET VB.NET web project that references a VB.NET class library. I

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.