Unplanned
Last Updated: 03 Feb 2023 09:24 by ADMIN
Raymond
Created on: 03 Feb 2023 09:16
Category: Grid
Type: Bug Report
1
Issue with DetailTableDataBind event Canceling or when passing Null as DataSource of DetailTableView

Hi,

I have a grid with detail tables.  When the user expands an entry in the parent table, the child table data gets populated via the DetailTableDataBind event.  That's all working properly when things go right.  I'm trying to implement something to address when the detail data retrieval fails.

I've tried setting Cancelled = true in the GridDetailTableBindEventArgs parameter, but that appears to do nothing.

What's even more strange is that if I don't set the value of DetailTableView.DataSource, or I set it to nothing, the value of DetailTableView.DataSource takes on the value of the parent item datasource.  That produces an exception because the parent table doesn't have the columns specified in the detail table DataKeyNames property.  If I clear the DataKeyNames, I can prevent the exception.  But then the child grid shows the parent grid records.

The following is a sample DetailTableDataBind Event:  What can I do to either prevent the parent item from opening, show an empty child item, etc?

 

Protected Sub rgGrid_DetailTableDataBind(sender As Object, e As GridDetailTableDataBindEventArgs) Handles rgErrorGrid.DetailTableDataBind
        Select Case e.DetailTableView.Name
            Case "DetailTableName"
                Dim dataItem As GridDataItem = TryCast(e.DetailTableView.ParentItem, GridDataItem)
                Dim KeyVal As Integer
                Dim dt As DataTable = Nothing
 
                If Integer.TryParse(dataItem.GetDataKeyValue("ID").ToString(), KeyVal) Then
                    dt = GetDetailTable(KeyVal)
                End If
 
                If dt Is Nothing Then
                    ' What do I need to do here to either prevent the parent item from expanding, or show an empty grid...without throwing an exception?
                    e.DetailTableView.DataSource = Nothing
                    e.Canceled = True
                Else
                    e.DetailTableView.DataSource = dt
                End If
        End Select
    End Sub


1 comment
ADMIN
Doncho
Posted on: 03 Feb 2023 09:24

The DetailTableView expects certain columns to be defined in the data source bound to it (at least the ones set as DataKeyNames).

A possible approach to overcome the issue is to assign an empty DataTable that has the DataKeyNames of the DetailTableView as columns instead of setting Nothing.

For instance:

If dt Is Nothing Then
    Dim emptyTable As DataTable = New DataTable()
    emptyTable.Columns.Add("DataKeyName1", Type.GetType("System.Int32"))
    emptyTable.Columns.Add("DataKeyName2", Type.GetType("System.Int32"))
    e.DetailTableView.DataSource = emptyTable
Else
    e.DetailTableView.DataSource = dt
End If


Kind regards,
Doncho
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.