BindingListView icon indicating copy to clipboard operation
BindingListView copied to clipboard

Issue when property value is null

Open mcanti opened this issue 9 years ago • 4 comments

Hello,

I have a column which binds to images stored as byte[]. When one row contains NULL in this property, an exception is thrown from AggregateBindingListView.cs, method CreateProvidedView(). The cause is that Activator.CreateInstance(viewType, list) doesn't know which constructor to take, given that list is null.

Please provide a fix for this case.

Thanks, Cantemir

mcanti avatar Dec 01 '16 11:12 mcanti

Hi @mcanti

I'd like to help you with this problem, but I have not looked at this code in a very long time. You are welcome to fork this repository, change the code and submit a pull request with the fix.

I am not the original creator of this code - I just copied it from the original SourceForge.net repo to GitHub.com because I don't like using SourceForge. The original creator, Andrew Davey is also on github but he no longer maintains the code.

I did take a brief look at the issue today and I put some notes below for you to consider. You could try changing the code to something like this and see if it works for you:

protected internal object CreateProvidedView(ObjectView<T> @object, PropertyDescriptor sourceListProperty)
{
    object list = sourceListProperty.GetValue(@object);
    if (list == null) {
        // The only caller to this function stores this value in a Dictionary<string,object>.
        // However, the code that pulls the value from the dictionary might have an error.
        // Please let me know if you find out! -wayne
        return null;
    }
    Type viewType = GetProvidedViewType(sourceListProperty);
    return Activator.CreateInstance(viewType, list);
}

Good luck!

waynebloss avatar Dec 01 '16 19:12 waynebloss

You might also want to ask the guy that creates ObjectListView since he probably knows a lot about winforms data binding. He's also on github

waynebloss avatar Dec 01 '16 19:12 waynebloss

Also, you might want to look at this solution, but I'm not sure if it's relevant to your situation - http://stackoverflow.com/a/7404758/16387

If I were binding to an image, I think I'd have a default image anyway if the underlying column was null.

waynebloss avatar Dec 02 '16 00:12 waynebloss

Hi @waynebloss

many thanks for the reply! I managed to implement a quick fix in ObjectView.cs: before creating the ProvidedView, I check if the value of the property is null: if (ShouldProvideViewOf(prop) && prop.GetValue(this) != null)

Best regards, mcanti

mcanti avatar Dec 05 '16 06:12 mcanti