This is a migrated thread and some comments may be shown as answers.

RadGrid behaves strangely when DomainDataSource returns null dataset

3 Answers 55 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jeff Benson
Top achievements
Rank 1
Iron
Jeff Benson asked on 03 Nov 2011, 04:17 PM
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

3 Answers, 1 is accepted

Sort by
0
Accepted
Marin
Telerik team
answered on 08 Nov 2011, 04:24 PM
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
0
Jeff Benson
Top achievements
Rank 1
Iron
answered on 11 Nov 2011, 12:08 AM
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
0
Marin
Telerik team
answered on 14 Nov 2011, 04:37 PM
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
Tags
Grid
Asked by
Jeff Benson
Top achievements
Rank 1
Iron
Answers by
Marin
Telerik team
Jeff Benson
Top achievements
Rank 1
Iron
Share this question
or