1

If my array property has null items, then I can't open CollectionEditor from PropertyGrid. I get error with text 'component'. How can I fix it?

    public partial class Form1 : Form
    {
        public Test[] test { get; set; }

        public Form1()
        {
            InitializeComponent();

            test = new Test[5];
            test[2] = new Test() { Name = "2" };

            propertyGrid1.SelectedObject = this;
        }
    }

    [TypeConverter(typeof(ExpandableObjectConverter))]
    public class Test
    {
        public string Name { get; set; }
    }

Maybe I should override some methods in my custom CollectionEditor, but i don't know which

1 Answer 1

1

It depends exactly how you want to work around this, but if you just need to exclude the null values, then you can override the default ArrayEditor class, something like this;

// define the editor on the property
[Editor(typeof(MyArrayEditor), typeof(UITypeEditor))]
public Test[] test { get; set; }

...

public class MyArrayEditor : ArrayEditor
{
    public MyArrayEditor(Type type) : base(type)
    {
    }

    protected override object[] GetItems(object editValue)
    {
        // filter on the objects you want the array editor to use
        return base.GetItems(editValue).Where(i => i != null).ToArray();
    }
}
2
  • So I can't add or remove items. I get message 'Collection was of a fixed size' even if collection has no null items Commented Jan 8, 2023 at 7:55
  • @KageBunshin - You talked about CollectionEditor in your question, but you can use ArrayEditor instead Commented Jan 8, 2023 at 8:24

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.