Question

I have DataGridView binded on collection of some objects. In DataGridView, there's one ComboBox with list of names, I'm trying to add new object to collection on every selection. But problem is that AddNew() always call default constructor of child. :/ Here's a code:

public class Parent:BindingList<Child>

public Child ChildProperty{get;set;}

public new object AddNew()
    {
        return ChildProperty;
    }

public new void AddingNew(object sender, AddingNewEventArgs e)
    {
        ChildProperty = new Child(this);
        e.NewObject = ChildProperty;
    }

I must use parameterized constructor, because i need to pass parent to child.

Was it helpful?

Solution

By using the new keyword, you aren't overriding the base class' methods; instead, you're creating brand new methods in the derived class only, which are never called.

Instead, you should override the AddNewCore() method and return a new object.

EDIT: You also need to Add() the new instance to the collection in your override.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top