简体   繁体   中英

C# - DataGridView can't add row?

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.

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);
}

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();
    }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM