Last Updated: 08 Apr 2020 16:18 by ADMIN
Created on: 03 Jun 2019 07:44
Category: Grid
Type: Bug Report
Object cannot be cast to DBNull exception is thrown when grouping by Null values in RadGrid
When Grouping in RadGrid while EnableLinqExpressions is set to True and some of the records contain null values will throw the exception: Object cannot be cast to DBNull
1 comment
Attila Antal
Posted on: 03 Jun 2019 08:01
Thank you for taking the time to report this issue.

While a fix is delivered we can offer the following workaround.

1. Disabling Linq expressions (in case Linq expressions are not needed, this property can be turned off and will eliminate the error)

<telerik:RadGrid ID="RadGrid1" runat="server" EnableLinqExpressions="true">

2. Convert null values to string Objects (In case Linq expressions are needed, the workaround is to manually convert DBNull to String objects.)


protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    if (e.Item is GridGroupHeaderItem)
        GridGroupHeaderItem groupHeaderItem = e.Item as GridGroupHeaderItem;
        DataRowView drv = (groupHeaderItem.DataItem as DataRowView);
        DataRow dr = (drv.Row as DataRow);
        var groupItems = dr.ItemArray;
        for (int i = 0; i < groupItems.Length; i++)
            if (string.IsNullOrEmpty(groupItems[i].ToString()))
                // object{System.DBNull} to object{string} or you can change it to other, depending on the datatype of the Database field
                groupItems[i] = String.Empty;
        // assign back the fixed object to the Group item
        dr.ItemArray = groupItems;


Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As GridItemEventArgs)
    If TypeOf e.Item Is GridGroupHeaderItem Then
        Dim groupHeaderItem As GridGroupHeaderItem = TryCast(e.Item, GridGroupHeaderItem)
        Dim drv As DataRowView = (TryCast(groupHeaderItem.DataItem, DataRowView))
        Dim dr As DataRow = (TryCast(drv.Row, DataRow))
        Dim groupItems = dr.ItemArray
        For i As Integer = 0 To groupItems.Length - 1
            If String.IsNullOrEmpty(groupItems(i).ToString()) Then
                groupItems(i) = String.Empty
            End If
        dr.ItemArray = groupItems
    End If
End Sub

Please accept our apology for any inconvenience this may have caused.