Your Car class should have a parameterless constructor and your data source should be something like a BindingList
Change the Car class to this:
class Car { public string color { get; set ; } public int maxspeed { get; set; } public Car() { } public Car (string color, int maxspeed) { this.color = color; this.maxspeed = maxspeed; } }
And then bind something like this:
BindingList<Car> carList = new BindingList<Car>(); dataGridView1.DataSource = carList;
You can also use BindingSource for this, you do not have to, but BindingSource provides some additional functions that may sometimes be needed.
If for some reason you absolutely cannot add a constructor without parameters, you can handle adding a new binding source event and call the constructor parameterized by Car:
Binding Setting:
BindingList<Car> carList = new BindingList<Car>(); BindingSource bs = new BindingSource(); bs.DataSource = carList; bs.AddingNew += new AddingNewEventHandler(bindingSource_AddingNew); bs.AllowNew = true; dataGridView1.DataSource = bs;
And the handler code:
void bindingSource_AddingNew(object sender, AddingNewEventArgs e) { e.NewObject = new Car("",0); }
David hall
source share