RadGrid behaves strangely when DomainDataSource returns null dataset

4 posts, 1 answers
  1. Jeff Benson
    Jeff Benson avatar
    4 posts
    Member since:
    Apr 2010

    Posted 03 Nov 2011 Link to this post

    I've been struggling with this problem for over day. While I have figured out a workaround, I wonder if anyone can explain whether there is a better way to handle it.

    I have a RadGrid that is bound to a DomainDataSource. The DomainDataSource is linked to a service (LinqToEntitiesDomainService<T>) and specifically to a function in the service that returns IQueryable<Computer>, where "Computer" is an entity in our application.

    In testing, I found that when the service query returned one or more "Computers", RadGrid behaves fine. However, the query function was coded (in C#) to return null when no "Computers" matched certain criteria. In this case, RadGrid did not behave as I expected. The RadGrid control would either appear as a single horizontal borderline on my page (i.e., it apparently collapses to zero height). If I set a fixed height on the control, it would then appear as an empty box.  I *thought* RadGrid was supposed to display my NoRecordsTemplate when the dataset was empty OR null. 

    My workaround (discovered by accident) was to change the service function so it returns a deliberately empty query. Lo and behold, now RadGrid displays my NoRecordsTemplate exactly as intended!

    What's the deal with RadGrid and a null (not empty) dataset? Is this expected behavior? If so, how did I miss that in the documentation?

    I am not particularly happy about fiddling with the service function logic. I wonder if there is another workaround I can make to handle a null dataset? For instance, can I handle one of the RadGrid events and substitute an empty collection of some sort?

    Thanks for any insights.

    Jeff
  2. Answer
    Marin
    Admin
    Marin avatar
    1045 posts

    Posted 08 Nov 2011 Link to this post

    Hi Jeff,

     This is the standard behavior for RadGrid when you try to bind it to null the binding does not happen at all hence there is nothing to render. The NoRecordsTemplate only shows when you don't have any records in the datasource after the grid tries to bind to it. There is a certain difference between no records in the datasource and no datasource at all which is reflected in the grid by not rendering at all in the latter case. That is why we recommend always binding to empty collection instead of null.

    Greetings,
    Marin
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Jeff Benson
    Jeff Benson avatar
    4 posts
    Member since:
    Apr 2010

    Posted 10 Nov 2011 Link to this post

    Marin,

    Thanks for confirming my observations.

    I do have another question that is directly related. Is there any way to detect when the grid has been bound to a null dataset? I would like to take advantage of the grid's standard behavior. Right now I have implemented a handler for the 'DataBound' event. Inside the handler I look at the Items collection in RadGrid and if the items count is zero perform some logic on my page. But, I found that if I enable filtering on RadGrid, I can easily select a filter condition that reduces the items count to zero. Is there a grid property or event that will allow me to differentiate between a null dataset and an empty collection? Bear in mind that my RadGrid is bound to an ASP.Net DomainDataSource on the page (via RadGrid DataSourceID property) and it is the DomainDataSource that is receiving the query result (which could be null) from our web service.

    I am new at ASP programming and apologize if the answer should be obvious.

    Thanks,

    Jeff
  5. Marin
    Admin
    Marin avatar
    1045 posts

    Posted 14 Nov 2011 Link to this post

    Hello Jeff,

     One difference in the behavior of the DomainDataSource is that when the service returns null the Queried event is not fired so you can use this to determine whether you are binding to null or an empty dataset:

    bool isDataBound = false;
     
            void DomainDataSource1_Queried(object sender, DomainDataSourceQueriedEventArgs e)
            {
                isDataBound = true;
            }
     
            void RadGrid1_DataBound(object sender, EventArgs e)
            {
                if (isDataBound)
                {
                    //zero or more records in the dataset
                }
                else
                {
                    //null - no dataset at all
                }
     
            }

    Hope this helps.

    Kind regards,
    Marin
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Back to Top