Dan

Dan

  • NA
  • 3
  • 0

DataGridViewComboBoxCell value is not valid.

Mar 21 2007 9:55 AM

I am attempting to become familiar with DataGridView class and in particular DataGridViewComboBoxColumn.  I created a sample program to this end but keep getting the following exception when running it:

 

  System.Argument.Exception: DataGridViewComboBoxCell value is not valid.


I created a DataGridView object via the designer and programatically added a DataGridViewComboBoxColumn setting the DataSource to a List<> of objects.

The exception appears to happen when the ComboBoxColumn object gains and then loses focus.  Also the value is never updated.  I handled the DataError event for kicks but it did not get me any additional information via the event argument.
 

    public partial class Form1 : Form

    {

        public Form1()

        {

            cars = new List<Car>();

            cars.Add(new Car("Chevy", "Monte Carlo", Color.Black));

            cars.Add(new Car("Ford", "Contour", Color.Blue));

 

            InitializeComponent();

 

            DataGridViewComboBoxColumn column = new DataGridViewComboBoxColumn();

            column.Name = "Cars";

            column.DataSource = cars;

 

            grid.Columns.Add(column);

        }

 

        private List<Car> cars;

    }

 

Here is the Car class (minus some implementation details).  I also implemented a TypeConverter to turn Car objects into strings and vica versa (although I'm not sure if this is really needed)..  

enum Color { Unknown, Black, Blue, Red, Yellow };

 

    [TypeConverter(typeof(CarConverter))]

    class Car

    {

        public Car()

            : this ("", "", Color.Unknown)

        {}

 

        public Car(string make, string model, Color color)

        {}

 

        public string Make

        {}

 

        public string Model

        {}

 

        public Color Color

        {}

 

        public override string ToString()

        {}

 

        private string make;

        private string model;

        private Color color;

    }

 

I must be doing something fundalmentally wrong, but after a couple of hours of searching I have yet to find any help specifically with DataGridViewComboBoxColumn using a list of objects as it's data source. Any help is appreciated.

 

Thanks,


Answers (1)