Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call parameterized constructor with AddNew()

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.

like image 529
user1997418 Avatar asked Apr 26 '26 15:04

user1997418


1 Answers

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.

like image 111
SLaks Avatar answered Apr 28 '26 07:04

SLaks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!