How To: RadGrid hierarchy from objects hierarchy

Posted by: the telerik blogs, on 11 Aug 2008 | View original | Bookmarked: 0 time(s)

Every once in a while we receive support about how to create hierarchical grid directly from objects hierarchy and I decided to post small example how to achieve this easily:

  protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
  {
    RadGrid1.DataSource = MyList;
  }

  protected void RadGrid1_DetailTableDataBind(object source, GridDetailTableDataBindEventArgs e)
  {
    e.DetailTableView.DataSource = MyList.Find(
         delegate(Master master)
         {
           return master.ID == Convert.ToInt32(e.DetailTableView.ParentItem.GetDataKeyValue("ID"));
         }
       ).Details;
  }

  public List<Master> MyList
  {
    get
    {
        List<Master> items = new List<Master>();
        for (int i = 0; i < 5; i++)
       {
           Master item = new Master();
           item.ID = i;
           item.Name = String.Format("Item{0}", i);

           List<Detail> detailList = new List<Detail>();
           for (int j = 0; j < 5; j++)
           {
             Detail detailItem = new Detail();
            detailItem.ID = j;
             detailItem.Name = String.Format("Item{0}", j);
             detailItem.MasterID = i;
             detailList.Add(detailItem);
           }
 
           item.Details = detailList;
           items.Add(item);
        }
 
        return items;
      }
  }

  public class Master
  {
   public int ID { get; set; }
   public string Name { get; set; }
   public List<Detail> Details { get; set; }
  }

  public class Detail : Master
  {
     public int MasterID { get; set; }
  }

...
        <telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource"
           OnDetailTableDataBind="RadGrid1_DetailTableDataBind">
             <MasterTableView DataKeyNames="ID">
                 <DetailTables>
                    <telerik:GridTableView Width="100%" runat="server" />
                 </DetailTables>
             </MasterTableView>
        </telerik:RadGrid>
...

The key here is to handle DetailTableDataBind event where you can find easily desired item in your collection a get item details.

Enjoy!

Advertisement
Free Agile Project Management Tool from Telerik
TeamPulse Community Edition helps your team effectively capture requirements, manage project plans, assign and track work, and most importantly, be continually connected with each other.
Category: ASP.NET | Other Posts: View all posts by this blogger | Report as irrelevant | View bloggers stats | Views: 2801 | Hits: 17

Similar Posts

  • The "Error creating window handle" exception and the Desktop Heap more
  • Handling concurrent database operations with Telerik OpenAccess ORM more
  • WPF Release History : Q1 2009 (version 2009.1.312) more
  • Silverlight Release History : Q1 2009 (version 2009.1.312) more
  • Identity Maps more
  • How To: Telerik RadGrid for ASP.NET AJAX with ASP.NET MVC more
  • Tips & tricks: Grouping in RadGridView for WinForms more
  • Emulating Paging with RadGridView for WinForms and LINQ with 1 million records more
  • Overloading Entity Framework Methods: More GetObjectStateEntries more
  • Export and Printing RadGridView using Telerik Reporting more

News Categories

.NET | Agile | Ajax | Architecture | ASP.NET | BizTalk | C# | Certification | Data | DataGrid | DataSet | Debugger | DotNetNuke | Events | GridView | IIS | Indigo | JavaScript | Mobile | Mono | Patterns and Practices | Performance | Podcast | Refactor | Regex | Security | Sharepoint | Silverlight | Smart Client Applications | Software | SQL | VB.NET | Visual Studio | W3 | WCF | WinFx | WPF | WSE | XAML | XLinq | XML | XSD