11

I have a simple C# Windows Forms application which should display a DataGridView. As DataBinding I used an Object (selected a class called Car) and this is what it looks like:

class Car
{
    public string color { get; set ; }
    public int maxspeed { get; set; }

    public Car (string color, int maxspeed) {
        this.color = color;
        this.maxspeed = maxspeed;
    }
}

However, when I set the DataGridView property AllowUserToAddRows to true, there is still no little * which allows me to add rows.

Someone suggested to set carBindingSource.AllowAdd to true, however, when I do that, I get a MissingMethodException which says my constructor could not be found.

2
  • I think you will need a parameter less constructor (since no info is available to be passed to your 2 param constructor), Well how are you binding it ? Commented Jul 9, 2012 at 11:25
  • I had to use AllowNew on my binding source. Commented May 7, 2015 at 13:49

3 Answers 3

16

Your Car class needs to have a parameterless constructor and your datasource needs 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 a BindingSource for this, you don't have to but BindingSource gives some extra functionality that can sometimes be necessary.


If for some reason you absolutely cannot add the parameterless constructor then you can handle the adding new event of the binding source and call the Car parameterised constructor:

Setting up the binding:

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

BindingList solved my problem. dataGridView1.DataSource = new BindingList<Car>(carList);
5

You need to add AddingNew event handler:

public partial class Form1 : Form
{
    private BindingSource carBindingSource = new BindingSource();



    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        dataGridView1.DataSource = carBindingSource;

        this.carBindingSource.AddingNew +=
        new AddingNewEventHandler(carBindingSource_AddingNew);

        carBindingSource.AllowNew = true;



    }

    void carBindingSource_AddingNew(object sender, AddingNewEventArgs e)
    {
        e.NewObject = new Car();
    }
}

Comments

1

I recently discovered that if you implement your own binding list using IBindingList you MUST return true in your SupportsChangeNotification in addition to AllowNew.

The MSDN article for DataGridView.AllowUserToAddRows only specifies the following remark however:

If the DataGridView is bound to data, the user is allowed to add rows if both this property and the data source's IBindingList.AllowNew property are set to true.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.