문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top