Display data hierarchy in the RadGridView

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

Sometimes it can be valuable to display hierarchy data in a grid. It can allow users to quickly drill down into the data at the click of a button. Below you will see a sample database with just three tables.

image

Notice that this is very elementary example, but it works well to demonstrate how easy it is to leverage hierarchy within the RadGridView control. I am using LINQToSQL to pull the data from the database. Below you will see the classes created after I did a drag and drop of the tables using Server Explorer in Visual Studio 2008.

image

Now I can add a new Window Form called "ClassInfo" to my project and drop a RadGridView onto to design surface. I set it the Dock property to Fill so you can see the grid appropriately. I added the following using statement to keep minimize my typing.

using Telerik.WinControls.UI;

The Load event is where I will populate the grid and setup the hierarchy relationship within the RadGridView.

privatevoidClassInfo_Load(objectsender,EventArgse)
{
//unrelatedsettings
classesGrid.MasterGridViewTemplate.AllowAddNewRow=false;
classesGrid.MasterGridViewTemplate.AutoSizeColumnsMode=GridViewAutoSizeColumnsMode.Fill;
using(varsampleDB=newSampleDataDataContext())
{
//themainsourcedata
classesGrid.DataSource=fromtinsampleDB.Teachers
wheret.Classes.Count()>0
selectnew{t.TeacherID,t.Name,t.Subject,ClassSize=t.Classes.Count()};
varchildDataTemplate=newGridViewTemplate()
{
AutoSizeColumnsMode=GridViewAutoSizeColumnsMode.Fill,
AllowAddNewRow=false,
DataSource=fromcinsampleDB.Classes
selectnew{c.TeacherID,c.RoomNumber,c.Student.Name,c.Student.Grade,c.Student.Age}
};
classesGrid.MasterGridViewTemplate.ChildGridViewTemplates.Add(childDataTemplate);
//definetherelationshipbetweenthedatasources
varparentChildRelationship=newGridViewRelation(classesGrid.MasterGridViewTemplate);
parentChildRelationship.ChildTemplate=childDataTemplate;
parentChildRelationship.RelationName="TeachersStudents";
parentChildRelationship.ParentColumnNames.Add("TeacherID");
parentChildRelationship.ChildColumnNames.Add("TeacherID");
//addtherelationshiptothegridview
classesGrid.Relations.Add(parentChildRelationship);
}
}

The end result is my grid will now display both the Teacher information and the Students that are in that Teachers classes. See below.

image

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: GridView | Other Posts: View all posts by this blogger | Report as irrelevant | View bloggers stats | Views: 2621 | Hits: 24

Similar Posts

  • Introducing Versatile DataSources more
  • How to display data from different tables using one data source more
  • How To: Silverlight grid hierarchy load on demand using MVVM and RIA services more
  • Announcing Microsoft Ajax Library (Preview 6) and the Microsoft Ajax Minifier more
  • LINQ to SQL, Lazy Loading and Prefetching more
  • Using Microsoft's Chart Controls In An ASP.NET Application: Programmatically Generating Chart Images more
  • Project Turing: Beginning RIA Svcs more
  • An alternative to Crystal more
  • How To Cache Rows In DevExpress ASP.NET GridView more
  • Tell the Debugger to Ignore Silverlight Validation Exceptions 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