Saturday, April 9, 2011

Telerik RadGrid doesn't fire NeedDataSource event on Rebind()

So you're using telerik, and you want your NeedDataSource method to get called when you rebind. But it doesnt?

This is a problem I face every now and then. Each time I struggle to remember the resolution from last time but keep forgetting. Just got the same error so this time, decided to put it down here.

Assumption:
An event handler for the event NeedDataSource has been properly subscribed.

i.e.:

Markup:

<telerik:RadGrid ... OnNeedDataSource="Gird_NeedDataSource">
...
</telerik:RadGrid>


Code behind:

protected void Grid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
{
// Logic to load data
}

private void SomeMethod()
{
...
Grid.Rebind();
...
}


Answer:

make the datasource of the grid null before calling rebind.
i.e.
Grid.DataSource = null;
Grid.Rebind();

Description:
Most likely you are not adhering to the standard way the RadGrid is supposed to be implemented (which is OK in my opinion).

If you place a break point in your existing NeedDataSource event handler, you will notice that it gets called before you explicitly call Rebind.

This means you most probably set the Grid.DataSource property within the NeedDataSource event. Therefore when the DataSource property is not null, that should mean that there is 'NO need for a data source'.


Therefore quite naturally the NeedDataSource event will not get fired when you rebind the grid.
I believe this is the expected behavior of the RadGrid. If you ever have a need to explicitly set the DataSource to null before calling rebind, that means something is not following the expected standard in your code. Oh well, you have to live with it right?

If you know of any other reasons as to why the NeedDataSource wouldn't get called on rebinding the grid, feel free to post a comment on it :)

1 comment: