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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:32:48+00:00 2026-05-11T00:32:48+00:00

So, I have a DataGridView using as datasource a BindingList DataGridView.DataSource = new BindingList<Car>{…}

  • 0

So, I have a DataGridView using as datasource a BindingList

DataGridView.DataSource = new  BindingList<Car>{...} 

Where

public class Car {     public ColorName Color { get; set;} } 

with

public class ColorName {     public int Id {get; set;}     public string Name{get; set;} } 

and I use a Combobox column:

DataGridViewComboBoxColumn colorNameDataGridViewTextBoxColumn; colorNameDataGridViewTextBoxColumn.DataPropertyName = 'Color'; colorNameDataGridViewTextBoxColumn.HeaderText = 'Color'; colorNameDataGridViewTextBoxColumn.Name = 'Color'; colorNameDataGridViewTextBoxColumn.DisplayMember = 'Name'; colorNameDataGridViewTextBoxColumn.ValueMember = 'Id'; colorNameDataGridViewTextBoxColumn.DataSource = new ColorName[] {...}; 

How can I get this to work ?! Now I get an exception because I think it tries to cast the Id to ColorName.

I tried with an empty ValueMember or adding a direct cast operator to ColorName class but can’t get it to work.

Sure I can use an int in the Car class to represent the color but is not as nice.

As you probably guessed those classes are in fact Castle Project ActiveRecord-s.

Any ideas are welcome !

  • 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. 2026-05-11T00:32:49+00:00Added an answer on May 11, 2026 at 12:32 am

    Did you try ValueMember = ” or ValueMember = ‘.’?

    Really hacky, but you could add a property on ColorName that is itself? (perhaps via a partial class)

    public ColorName Self {get {return this;}} 

    then set `ValueMember = ‘Self’;’

    Other than that, you’d probably need a TypeConverter

    The other option might be to override ToString() on ColorName to return Name, and not have a value/display member?


    (update: no it doesn’t)

    Have checked, and ToString() seems to work :

    public override string ToString() { return Name; } 

    and just don’t set a DisplayMember or a ValueMember.


    Well whad’ya know – the ‘Self’ trick works too …

    using System; using System.ComponentModel; using System.Windows.Forms; class ColorName {     public ColorName(int id, string name) {         this.Id = id;         this.Name = name;     }     public int Id { get; private set; }     public string Name { get; private set; }      // maybe declare this one in a partial class...     public ColorName Self { get { return this; } } } class Car {     public ColorName Color { get; set; } }  static class Program {     [STAThread]     static void Main()     {         Application.EnableVisualStyles();         using(Form form = new Form())         using (DataGridView grid = new DataGridView())         {             grid.Dock = DockStyle.Fill;             grid.AutoGenerateColumns = false;             ColorName[] colors = new[] {               new ColorName(1,'Red'),               new ColorName(2,'Blue'),               new ColorName(3,'Green')             };             var col = new DataGridViewComboBoxColumn             {                 DataPropertyName = 'Color',                 HeaderText = 'Color',                 Name = 'Color',                 DisplayMember = 'Name',                 ValueMember = 'Self',                 DataSource = colors             };              grid.Columns.Add(col);             grid.DataSource = new BindingList<Car> {                 new Car { Color = colors[0]}             };             form.Controls.Add(grid);             Application.Run(form);         }     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 118k
  • Answers 118k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer MVC is an interesting abstraction, but has some problems. In… May 11, 2026 at 11:40 pm
  • Editorial Team
    Editorial Team added an answer I would say it makes no sense to modify a… May 11, 2026 at 11:40 pm
  • Editorial Team
    Editorial Team added an answer This is key strengthening (http://en.wikipedia.org/wiki/Key_strengthening), a nice technique that nonetheless… May 11, 2026 at 11:40 pm

Related Questions

So, I have a DataGridView using as datasource a BindingList DataGridView.DataSource = new BindingList<Car>{...}
I have a custom object that implements INotifyPropertyChanged. I have a collection of these
I have a DataGridView with its datasource set to a generic list of custom
I have a three-tier Windows Forms DB application in VB.NET. I'm using VS 2005.
I have a DataTable which is populated from a CSV file then, using a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.