GridView can't handle DBNull in dynamic data row

2 posts, 0 answers
  1. Patrick
    Patrick avatar
    55 posts
    Member since:
    Jun 2008

    Posted 25 Aug Link to this post

    I am dynamically loading a small amount of data from a Sql Server table using the dynamic data row concept in this example.
    Everything is working however if one of the values is DBNull then when the column is sorted the grid displays as blank. Toggling the sort back to off shows the data again.

    To solve this I am simply casting to null when the value is DBNull.

    var value = reader.GetValue(x);
                         
    row[column] = value == DBNull.Value
        ? null
        : value;

     

    This sounds to me like a bug.

  2. Stefan X1
    Admin
    Stefan X1 avatar
    523 posts

    Posted 29 Aug Link to this post

    Hello Patrick,

    The reported behavior is due to a LINQ exception caused by a type mismatch. Generally, when a DynamicObject is used, the data engine resolves the data type of a given property based on the data type of the relevant property of the first item. So, in this particular scenario, it tries to apply an object of type DBNull to a property of type String. Thus, an ArgumentException is raised and internally caught.

    On the other hand, applying a null value to it a string property is a valid operation, thus the control is working as expected. I am afraid that there is not much we can do for this scenario, so I suggest you sticking to the approach you've already adopted.

    All the best,
    Stefan X1
    Telerik by Progress
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  3. UI for WPF is Visual Studio 2017 Ready
Back to Top