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 246483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:10:35+00:00 2026-05-11T21:10:35+00:00

I have a grid, and I’m setting the DataSource to a List<IListItem> . What

  • 0

I have a grid, and I’m setting the DataSource to a List<IListItem>. What I want is to have the list bind to the underlying type, and disply those properties, rather than the properties defined in IListItem. So:

public interface IListItem
{
    string Id;
    string Name;
}

public class User : IListItem
{
    string Id { get; set; };
    string Name { get; set; };
    string UserSpecificField { get; set; };
}

public class Location : IListItem
{
    string Id { get; set; };
    string Name { get; set; };
    string LocationSpecificField { get; set; };
}

How do I bind to a grid so that if my List<IListItem> contains users I will see the user-specific field? Edit: Note that any given list I want to bind to the Datagrid will be comprised of a single underlying type.

  • 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-11T21:10:35+00:00Added an answer on May 11, 2026 at 9:10 pm

    Data-binding to lists follows the following strategy:

    1. does the data-source implement IListSource? if so, goto 2 with the result of GetList()
    2. does the data-source implement IList? if not, throw an error; list expected
    3. does the data-source implement ITypedList? if so use this for metadata (exit)
    4. does the data-source have a non-object indexer, public Foo this[int index] (for some Foo)? if so, use typeof(Foo) for metadata
    5. is there anything in the list? if so, use the first item (list[0]) for metadata
    6. no metadata available

    List<IListItem> falls into “4” above, since it has a typed indexer of type IListItem – and so it will get the metadata via TypeDescriptor.GetProperties(typeof(IListItem)).

    So now, you have three options:

    • write a TypeDescriptionProvider that returns the properties for IListItem – I’m not sure this is feasible since you can’t possibly know what the concrete type is given just IListItem
    • use the correctly typed list (List<User> etc) – simply as a simple way of getting an IList with a non-object indexer
    • write an ITypedList wrapper (lots of work)
    • use something like ArrayList (i.e. no public non-object indexer) – very hacky!

    My preference is for using the correct type of List<>… here’s an AutoCast method that does this for you without having to know the types (with sample usage);

    Note that this only works for homogeneous data (i.e. all the objects are the same), and it requires at least one object in the list to infer the type…

    // infers the correct list type from the contents
    static IList AutoCast(this IList list) {
        if (list == null) throw new ArgumentNullException("list");
        if (list.Count == 0) throw new InvalidOperationException(
              "Cannot AutoCast an empty list");
        Type type = list[0].GetType();
        IList result = (IList) Activator.CreateInstance(typeof(List<>)
              .MakeGenericType(type), list.Count);
        foreach (object obj in list) result.Add(obj);
        return result;
    }
    // usage
    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        List<IListItem> data = new List<IListItem> {
            new User { Id = "1", Name = "abc", UserSpecificField = "def"},
            new User { Id = "2", Name = "ghi", UserSpecificField = "jkl"},
        };
        ShowData(data, "Before change - no UserSpecifiedField");
        ShowData(data.AutoCast(), "After change - has UserSpecifiedField");
    }
    static void ShowData(object dataSource, string caption) {
        Application.Run(new Form {
            Text = caption,
            Controls = {
                new DataGridView {
                    Dock = DockStyle.Fill,
                    DataSource = dataSource,
                    AllowUserToAddRows = false,
                    AllowUserToDeleteRows = false
                }
            }
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have grid that uses around 12 to 15 label when i bind it...and
I have Grid with 10 rows and 10 column, and I want in code
I have grid of button. I want to a button to be clicked(invoke click
I have a grid laid out like so; +---------+---------+ | Image | Details |
Suppose I have a grid with some row definitions, and a child control in
Here's the scenario I have a Grid with some TextBlock controls, each in a
I have an ExtJS grid on a web page and I'd like to save
I have a Property Grid in C#, loading up a 'PropertyAdapter' object (a basic
I have two different grid controls on the same form. They share the same
I have a Data Grid View inside a control that is displayed in a

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.