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

bind datasource to nested radgrid codebehind

1 Answer 285 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Mike
Top achievements
Rank 1
Mike asked on 11 Apr 2013, 08:34 PM
I'm trying to follow along with this demo
http://demos.telerik.com/aspnet-ajax/grid/examples/hierarchy/nestedviewtemplate/defaultcs.aspx

However I want to bind the datasource of the first nested radgrid on the code behind.

I thought it might be something like this but is it more complicated than this?
RadGrid1.MasterTableView.DetailTables.radGrid2.DataSource = myDatasource;

Also another question, when I bind to this nested grid (radGrid2), I send all records and the following code will filter or do I need to grab the ID on a method call within the radGrid1? If so please direct me to how to do accomplish this?
<ParentTableRelation>
                                                <telerik:GridRelationFields DetailKeyField="referenceID" MasterKeyField="referenceID"></telerik:GridRelationFields>
                                            </ParentTableRelation>


overview,
radGrid1 shows all records, use the dropdown and show records in the radGrid2 that pertain to that ID of radGrid1.

I foudn the following but get an error : on this line - (GridTableView)item.ChildItem.NestedTableViews[0]

Index was outside the bounds of the array.



protected void RadGrid1_PreRender(object sender, EventArgs e)
     {
         using (var db = new E3TDataContext())
         {
             citationItems = db.Citations.ToList();
             //RadGrid1.MasterTableView.DetailTables.radGrid2.DataSource = myDatasource;
          
         foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
         {
             GridTableView nestedView = (GridTableView)item.ChildItem.NestedTableViews[0];
             if (item["parentrowColumnUniqueName"].Text == "Text")
                 nestedView.DataSource = citationItems;
             else if (item["parentrowColumnUniqueName"].Text != "Text")
                 nestedView.DataSource = citationItems;
         }
        }
     }

Thanks

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 12 Apr 2013, 05:26 AM
Hi,

You can bind the inner grid in its NeedDataSource event as shown below.
C#
protected void RadGrid2_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
  {
      GridDataItem parentItem = ((sender as RadGrid).NamingContainer as GridNestedViewItem).ParentItem as GridDataItem;
      (sender as RadGrid).DataSource = GetDataTable("SELECT OrderID, EmployeeID, CustomerID FROM Orders where CustomerID='" + parentItem.GetDataKeyValue("CustomerID").ToString() + "'");
  }
Thanks,
Shinu.
Tags
General Discussions
Asked by
Mike
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or