Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / ASP.NET > Grid > Change RadGrid datasource 'dynamically'
RadControls for ASP.NET are no longer supported (see this page for reference). In case you have inquiries about the Telerik ASP.NET AJAX controls, post them in the pertinent ASP.NET AJAX forums.

Change RadGrid datasource 'dynamically'

Feed from this thread
  • Posted on Aug 28, 2007 (permalink)

    I am trying to implement a simple navigation structure to access/modify a large number of tables.  I'm using a TreeView control to select a table to view, with the NodeClick event changing the DataSourceID of the grid.

    Code Snippet:

    protected void AdminTreeView_NodeClick(object o, Telerik.WebControls.RadTreeNodeEventArgs e)
    {
    DetailsGrid.DataSource = null;

    switch ((string)e.NodeClicked.Value)
    {
    case "contact":
    DetailsGrid.DataSourceID = "ContactsDS";
    break;
    case "schedule":
    DetailsGrid.DataSourceID = "SchedulesDS";
    break;
    etc...
    }
    DetailsGrid.Rebind();

    Basically this works as long as I have not sorted or grouped by any columns.  For example, I change DetailsGrid.DataSourceID to "ContactsDS", sort by contact_id, then change it to "SchedulesDS", I get the error "Cannot find column contact_id".

    It appears to persist the sort column or groupings through a change in datasources and is killing my app.  I simply need to clear this data before changing the dataset, but cannot find a good way to do so.  Any help would be greatly appreciated.  I've tried several things including DataBind(), Rebind(), GroupingEnabled = false, etc. but no luck.

    Thanks,
    Matt

  • Posted on Aug 29, 2007 (permalink)

    Hi,

    Try clearing the SortExpressions and GroupByExpressions  using the below code before changing the DataSource.

    DetailsGrid.MasterTableView.SortExpressions.Clear();
    DetailsGrid.MasterTableView.GroupByExpressions.Clear();


    CS:
    protected void AdminTreeView_NodeClick(object o, Telerik.WebControls.RadTreeNodeEventArgs e)
    {
    DetailsGrid.MasterTableView.SortExpressions.Clear();
    DetailsGrid.MasterTableView.GroupByExpressions.Clear();

    DetailsGrid.DataSource = null;

    switch ((string)e.NodeClicked.Value)
    {
    case "contact":
      DetailsGrid.DataSourceID = "ContactsDS";
    break;
    case "schedule":
    DetailsGrid.DataSourceID = "SchedulesDS";
    break;

    }


    Hope this helps.

    Shinu.

  • Posted on Aug 29, 2007 (permalink)

    Thank you Shinu, that fixed my problem perfectly.

    Matt

  • Srujana avatar

    Posted on Jun 23, 2011 (permalink)

    Hello Telerik Team,

    I am trying to build almost same functionlity that of original post, additionally  I need to add columns programatically(on code behind), and I need to have sorting and paging enabled. I just don't want to use autogeneratecolumn=true, and need to display just required columns in each datasource.

    Could you please give some direction on how I can change the set of columns to display in each datasource.

    Thanks in advance,
    SC.

  • Tsvetoslav Tsvetoslav admin's avatar

    Posted on Jun 24, 2011 (permalink)

    Hello Srujana,

    Do take a look at the following online example on how to programmatically create a RadGrid control:
    http://demos.telerik.com/aspnet-ajax/grid/examples/hierarchy/definingstructure/defaultcs.aspx

    In addition, in your scenario you need to clear the placeholder's Controls collection and recreate the grid anew each time the data-source is changed. Let's suppose for example that you need to recreate the grid each time a data-source table is selected from the treeview. Now in the OnNodeClicked event of the TreeView you need to clear the placeholder's Controls collection and recreate the grid as per the guidelines in the online example - then add the grid again to the placeholder's Controls collection

    Hope it helps.

    Best wishes,
    Tsvetoslav
    the Telerik team

    Consider using RadControls for ASP.NET AJAX (built on top of the ASP.NET AJAX framework) as a replacement for the Telerik ASP.NET Classic controls, See the product support lifecycle here.

  • Srujana avatar

    Posted on Jun 24, 2011 (permalink)

    Thanks for the quick response.

    Yes, I was able to build the grid with different datasources, but when I try to implement sorting and paging the Grid goes blank, no data gets displayed, is there a way to rebind the Grid with same datasource after sorting or paging? What is the best way to do it?

    Thanks for your assistance.

    Srujana.

  • Sebastian Sebastian admin's avatar

    Posted on Jun 27, 2011 (permalink)

    Hi Srujana,

    Make sure you use either advanced data-binding with NeedDataSource/DetailTableDataBind event handling as illustrated here or assign data source controls for each level in the hierarchy. Note that hierarchy binding with simple DataBind() calls is not supported by our web grid.

    Kind regards,
    Sebastian
    the Telerik team

    Consider using RadControls for ASP.NET AJAX (built on top of the ASP.NET AJAX framework) as a replacement for the Telerik ASP.NET Classic controls, See the product support lifecycle here.

Back to Top

Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / ASP.NET > Grid > Change RadGrid datasource 'dynamically'