Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Blogs | Beginners | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Team Foundation Server Hosting
Search :       Advanced Search »
Home » Silverlight » Show Child Grid inside Grid in Silverlight

Show Child Grid inside Grid in Silverlight

This article describes you the concept of Data Grid. It shows how use a child grid inside of a Data Grid.

Author Rank :
Page Views : 5544
Downloads : 113
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
ChildGridInsideGrid.zip
 
 
Team Foundation Server Hosting
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


This article describes for you the concept of Data Grid. It shows use a child grid inside of a Data Grid.

So here we go. First of all make a new Silverlight project and put a name and save location.

Image1.jpg
 

Image 1.

Now add a two new classes using Add new Item menu and put a suitable class name.

Customers.cs

Add a namespace on top of class.

using System.Collections.Generic;

public class Customers
{
    /// <summary>
    /// Represents the customer's id.
    /// </summary>
    public string CustomerID { get; set; }
    /// <summary>
    /// Represents the customer's company name.
    /// </summary>
    public string CompanyName { get; set; }
    /// <summary>
    /// Represents the customer's contact name.
    /// </summary>
    public string ContactName { get; set; }
    /// <summary>
    /// Represents the customer's contact title.
    /// </summary>
    public string ContactTitle { get; set; }
    /// <summary>
    /// Represents the customer's address.
    /// </summary>
    public string Address { get; set; }
    /// <summary>
    /// Represents the customer's city.
    /// </summary>
    public string City { get; set; }
    /// <summary>
    /// Represents the customer's region.
    /// </summary>
    public string Region { get; set; }
    /// <summary>
    /// Gets customer information
    /// </summary>
    public static List<Customers> GetCustomers()
    {
        return new List<Customers>
        { 
            new Customers
            {
                CustomerID="ALFKI",
                CompanyName = "Berglunds snabbköp",
                ContactName = "Christina Berglund",
                ContactTitle = "Order Administrator",
                Address = "Berguvsvägen  8",
                City = "LuleÃ¥",
                Region = "WA"                       
            },
            new Customers
            {
                CustomerID="ANATR",
                CompanyName = "Blauer See Delikatessen",
                ContactName = "Hanna Moos",
                ContactTitle = "Sales Representative",
                Address = "Forsterstr. 57",
                City = "Mannheim",
                Region = "hgj"                      
            },
            new Customers
            {
                CustomerID="AROUT",
                CompanyName = "Blondesddsl père et fils",
                ContactName = "Frédérique Citeaux",
                ContactTitle = "Marketing Manager",
                Address = "24, place Kléber",
                City = "Strasbourg",
                Region = "hg"                     
            },
            new Customers
            {
                CustomerID="BERGS",
                CompanyName = "Bólido Comidas preparadas",
                ContactName = "Martà n Sommer",
                ContactTitle = "Owner",
                Address = "C/ Araquil, 67",
                City = "Madrid",
                Region = "jgk"                     
            },
            new Customers
            {
                CustomerID="BLAUS",
                CompanyName = "Bottom-Dollar Markets",
                ContactName = "Elizabeth Lincoln",
                ContactTitle = "Accounting Manager",
                Address = "23 Tsawassen Blvd.",
                City = "Tsawassen",
                Region = "BC"
            },
            new Customers
            {
                CustomerID="BLONP",
                CompanyName = "Laughing Bacchus Wine Cellars",
                ContactName = "Yoshi Tannamuri",
                ContactTitle = "Marketing Assistant",
                Address = "1900 Oak St.",
                City = "Vancouver",
                Region = "BC"                      
            },
            new Customers
            {
                CustomerID="BOLID",
                CompanyName = "Lazy K Kountry Store",
                ContactName = "John Steel",
                ContactTitle = "Marketing Manager",
                Address = "12 Orchestra Terrace",
                City = "Walla Walla",
                Region = "WA"                       
            },
            new Customers
            {
                CustomerID="BONAP",
                CompanyName = "LILA-Supermercado",
                ContactName = "Carlos González",
                ContactTitle = "Accounting Manager",
                Address = "Carrera 52 con Ave. Bolà var #65-98 Llano Largo",
                City = "Barquisimeto",
                Region = "Lara"                       
            }                              
      };
    }       
}

Here is my second class which is for child grid.

CustomerMoreDetails.cs

public class CustomerMoreDetails
{
    /// <summary>
    /// Represents the customer's id.
    /// </summary>
    public string CustomerID { get; set; }
    /// <summary>
    /// Represents the customer's postal code.
    /// </summary>
    public string PostalCode { get; set; }
    /// <summary>
    /// Represents the customer's country.
    /// </summary>
    public string Country { get; set; }
    /// <summary>
    /// Represents the customer's phone.
    /// </summary>
    public string Phone { get; set; }
    /// <summary>
    /// Represents the customer's fax number.
    /// </summary>
    public string Fax { get; set; }
    public static List<CustomerMoreDetails> GetCustomerDetail()
    {
        return new List<CustomerMoreDetails>
        {
            new CustomerMoreDetails
            {
                CustomerID="ALFKI",
                PostalCode = "12345",
                Country = "Maxico",
                Phone = "(5) 555-4729",
                Fax = "(5) 555-4729"
            },
            new CustomerMoreDetails
            {
                CustomerID="ALFKI",
                PostalCode = "12345",
                Country = "Shri Lanka",
                Phone = "(5) 555-4729",
                Fax = "(5) 555-4729"
            },
            new CustomerMoreDetails
            {
                CustomerID="ALFKI",
                PostalCode = "12345",
                Country = "India",
                Phone = "(5) 555-4729",
                Fax = "(5) 555-4729"
            },
            new CustomerMoreDetails
            {
                CustomerID="ANATR",
                PostalCode = "12345",
                Country = "US",
                Phone = "(5) 555-4729",
                Fax = "(5) 555-4729"
            },
            new CustomerMoreDetails
            {
                CustomerID="AROUT",
                PostalCode = "12345",
                Country = "Canada",
                Phone = "(5) 555-4729",
                Fax = "(5) 555-4729"
            },
            new CustomerMoreDetails
            {
                CustomerID="BERGS",
                PostalCode = "12345",
                Country = "Australia",
                Phone = "(5) 555-4729",
                Fax = "(5) 555-4729"
            },
            new CustomerMoreDetails
            {
                CustomerID="BLAUS",
                PostalCode = "12345",
                Country = "ENgland",
                Phone = "(5) 555-4729",
                Fax = "(5) 555-4729"
            },
            new CustomerMoreDetails
            {
                CustomerID="BLONP",
                PostalCode = "12345",
                Country = "Pakistan",
                Phone = "(5) 555-4729",
                Fax = "(5) 555-4729"
            },
            new CustomerMoreDetails
            {
                CustomerID="BOLID",
                PostalCode = "12345",
                Country = "Brazil",
                Phone = "(5) 555-4729",
                Fax = "(5) 555-4729"
            },
            new CustomerMoreDetails
            {
                CustomerID="BONAP",
                PostalCode = "12345",
                Country = "Newzealand",
                Phone = "(5) 555-4729",
                Fax = "(5) 555-4729"
            }
        };
    }
}

Now let's work on MailPage.xaml...

First of all a new reference of System.Windows.Controls.data and call that assembly on page.

xmlns:dataGrid="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"

<Grid x:Name="LayoutRoot" Background="White">
<dataGrid:DataGrid Margin="5,5,5,0" x:Name="customersDataGrid" Width="800"  GridLinesVisibility="Horizontal" AutoGenerateColumns="False"
VerticalAlignment="Top" RowDetailsVisibilityChanged="customersDataGrid_RowDetailsVisibilityChanged" IsReadOnly="True" >
            <dataGrid:DataGrid.Columns>
                <dataGrid:DataGridTextColumn Header="Customer ID" Width="Auto" Binding="{Binding CustomerID}" CanUserReorder="False"/>
                <dataGrid:DataGridTextColumn Header="Company Name" Width="Auto" Binding="{Binding CompanyName}"  CanUserReorder="False" />
                <dataGrid:DataGridTextColumn Header="Contact Name" Width="Auto" Binding="{Binding ContactName}" CanUserReorder="False"/>
                <dataGrid:DataGridTextColumn Header="Contact Title" Width="Auto" Binding="{Binding ContactTitle}" CanUserReorder="False"/>
                <dataGrid:DataGridTextColumn Header="Address" Width="Auto" Binding="{Binding Address}"  CanUserReorder="False"/>
                <dataGrid:DataGridTextColumn Header="City" Width="Auto" Binding="{Binding City}"  CanUserReorder="False"/>
                <dataGrid:DataGridTextColumn Header="Region" Width="Auto" Binding="{Binding Region}"  CanUserReorder="False"/>               
            </dataGrid:DataGrid.Columns>
            
This is Child grid 

<dataGrid:DataGrid.RowDetailsTemplate>
                <DataTemplate>
                    <dataGrid:DataGrid x:Name="customerDetail" Width="400" AutoGenerateColumns="False" HorizontalAlignment="Center" IsReadOnly="True" Grid.Row="1">
                        <dataGrid:DataGrid.Columns>
                            <dataGrid:DataGridTextColumn Header="Customer ID" Binding="{Binding CustomerID}"/>
                            <dataGrid:DataGridTextColumn Header="Postal Code" Binding="{Binding PostalCode}"/>
                            <dataGrid:DataGridTextColumn Header="Country" Binding="{Binding Country}"/>
                            <dataGrid:DataGridTextColumn Header="Phone" Binding="{Binding Phone}"/>
                            <dataGrid:DataGridTextColumn Header="Fax" Binding="{Binding Fax}"/>
                        </dataGrid:DataGrid.Columns>
                    </dataGrid:DataGrid>
                </DataTemplate>
</dataGrid:DataGrid.RowDetailsTemplate>

Now bind these two grids.

List<CustomerMoreDetails> customerDetail = new List<CustomerMoreDetails>();
public MainPage()
{
    InitializeComponent();
    customersDataGrid.ItemsSource = Customers.GetCustomers();
    customerDetail = CustomerMoreDetails.GetCustomerDetail();
}
private void customersDataGrid_RowDetailsVisibilityChanged(object sender, DataGridRowDetailsEventArgs e)
{
    try
    {
        if (e.Row.DataContext != null)
        {
            var objFinalRecords = from tblCustomerDetail in customerDetail
                                         where tblCustomerDetail.CustomerID == ((Customers)e.Row.DataContext).CustomerID
                                         select tblCustomerDetail;
            ((DataGrid)e.DetailsElement).ItemsSource = objFinalRecords;
        }
    }
    catch (Exception Ex)
    {
        MessageBox.Show(Ex.Message, "Error", MessageBoxButton.OK);
    }
}

So finally run the application and see the result something like this.

Image2.jpg
 

Image 2.

When you click on a row then result should be like this.

Image3.jpg
 

Image 3.

Image4.jpg
 

Image 4.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Author
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Nevron Gauge for SharePoint
Become a Sponsor
 Comments
Team Foundation Server Hosting
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.