This example demonstrates how to bind the VIBlend DataGrid for Silverlight to a list of invoices that has nested properties. The schema of data source contains the following DataFields / Properties: SalesPerson(string), ShipperCompany(string), ProductName(string), UnitPrice(double),
Quantity(double), Discount(double), ExtendedPrice(double), Freight(double) and Address(Address). The address object has the following properties: City(string), Region(string) and Country(string).
We'll create a Pivot Table with 3 pivot rows - City, Country and Region and 1 pivot column - Shipper Company.
<viblend:DataGrid ItemsSource="{Binding}" x:Name="dataGrid1" Margin="4" Background="White" AutoGenerateColumns="True">
<viblend:DataGrid.BoundFields>
<viblend:BoundField Text="Country" DataField="Address.Country" Width="160" SortMode="NotSortable"/>
<viblend:BoundField Text="City" DataField="Address.City" Width="140" SortMode="NotSortable"/>
<viblend:BoundField Text="Region" DataField="Address.Region" Width="140" SortMode="NotSortable"/>
<viblend:BoundField Text="Carrier" DataField="ShipperCompany" Width="140" SortMode="NotSortable" />
<viblend:BoundField Text="ExtendedPrice" DataField="ExtendedPrice" Width="140" SortMode="NotSortable" />
</viblend:DataGrid.BoundFields>
<viblend:DataGrid.BoundPivotRows>
<viblend:BoundField Text="Country" DataField="Address.Country" Width="160" SortMode="NotSortable"/>
<viblend:BoundField Text="Region" DataField="Address.Region" Width="160" SortMode="NotSortable"/>
<viblend:BoundField Text="City" DataField="Address.City" Width="140" SortMode="NotSortable"/>
</viblend:DataGrid.BoundPivotRows>
<viblend:DataGrid.BoundPivotColumns>
<viblend:BoundField Text="Carrier" DataField="ShipperCompany" Width="140" SortMode="NotSortable"/>
</viblend:DataGrid.BoundPivotColumns>
<viblend:DataGrid.BoundPivotValues>
<viblend:BoundValueField Width="120" CellHorizontalContentAlignment="Center" Text="Count of Sales" Function="Count" DataField="ExtendedPrice"></viblend:BoundValueField>
<viblend:BoundValueField Width="120" CellHorizontalContentAlignment="Right" CellVerticalContentAlignment="Center" CellTextFormatString="{}{0:C}" Text="Amount of Sales" Function="Sum" DataField="ExtendedPrice"></viblend:BoundValueField>
<viblend:BoundValueField Width="120" CellHorizontalContentAlignment="Right" CellVerticalContentAlignment="Center" CellTextFormatString="{}{0:C}" Text="Avg Sale Amount" Function="Average" DataField="ExtendedPrice"></viblend:BoundValueField>
</viblend:DataGrid.BoundPivotValues>
</viblend:DataGrid>
The following code generates the sample data in the example:
C#
public PivotTableDemo()
{
InitializeComponent();
PrepareGridData();
this.DataContext = this.lst;
}
public class Invoice
{
public Invoice(
string City,
string Region,
string Country,
string SalesPerson,
string ShipperCompany,
string ProductName,
double UnitPrice,
double Quantity,
double Discount,
double ExtendedPrice,
double Freight)
{
this.SalesPerson = SalesPerson;
this.Address = new Address();
this.Address.City = City;
this.Address.Region = string.IsNullOrEmpty(Region) ? "All Regions" : Region;
this.Address.Country = Country;
this.ShipperCompany = ShipperCompany;
this.ProductName = ProductName;
this.UnitPrice = UnitPrice;
this.Quantity = Quantity;
this.Discount = Discount;
this.ExtendedPrice = ExtendedPrice;
this.Freight = Freight;
}
public string SalesPerson { get; set; }
public string ShipperCompany { get; set; }
public Address Address { get; set; }
public string ProductName { get; set; }
public double UnitPrice { get; set; }
public double Quantity { get; set; }
public double Discount { get; set; }
public double ExtendedPrice { get; set; }
public double Freight { get; set; }
}
public class Address
{
public string City { get; set; }
public string Region { get; set; }
public string Country { get; set; }
}
public List<Invoice> lst = new List<Invoice>();
private void PrepareGridData()
{
try
{
StreamResourceInfo sri = Application.GetResourceStream(new Uri("IntegratedDemo;component/Examples/Grid/invoices.tab", UriKind.Relative));
System.IO.StreamReader sr = new System.IO.StreamReader(sri.Stream);
string line = null;
sr.ReadLine();
while ((line = sr.ReadLine()) != null)
{
string[] tabs = line.Split('\t');
lst.Add(new Invoice(tabs[0], tabs[1], tabs[2], tabs[3], tabs[4], tabs[5], double.Parse(tabs[6]), double.Parse(tabs[7]), double.Parse(tabs[8]), double.Parse(tabs[9]), double.Parse(tabs[10])));
}
sr.Close();
}
catch (System.Exception)
{
}
}
VB .NET
Public Sub New()
InitializeComponent()
PrepareGridData()
Me.DataContext = Me.lst
End Sub
Public Class Invoice
Public Sub New(ByVal City As String, ByVal Region As String, ByVal Country As String, ByVal SalesPerson As String, ByVal ShipperCompany As String, ByVal ProductName As String, ByVal UnitPrice As Double, ByVal Quantity As Double, ByVal Discount As Double, ByVal ExtendedPrice As Double, ByVal Freight As Double)
Me.SalesPerson = SalesPerson
Me.Address = New Address()
Me.Address.City = City
Me.Address.Region = If(String.IsNullOrEmpty(Region), "All Regions", Region)
Me.Address.Country = Country
Me.ShipperCompany = ShipperCompany
Me.ProductName = ProductName
Me.UnitPrice = UnitPrice
Me.Quantity = Quantity
Me.Discount = Discount
Me.ExtendedPrice = ExtendedPrice
Me.Freight = Freight
End Sub
Private privateSalesPerson As String
Public Property SalesPerson() As String
Get
Return privateSalesPerson
End Get
Set(ByVal value As String)
privateSalesPerson = value
End Set
End Property
Private privateShipperCompany As String
Public Property ShipperCompany() As String
Get
Return privateShipperCompany
End Get
Set(ByVal value As String)
privateShipperCompany = value
End Set
End Property
Private privateAddress As Address
Public Property Address() As Address
Get
Return privateAddress
End Get
Set(ByVal value As Address)
privateAddress = value
End Set
End Property
Private privateProductName As String
Public Property ProductName() As String
Get
Return privateProductName
End Get
Set(ByVal value As String)
privateProductName = value
End Set
End Property
Private privateUnitPrice As Double
Public Property UnitPrice() As Double
Get
Return privateUnitPrice
End Get
Set(ByVal value As Double)
privateUnitPrice = value
End Set
End Property
Private privateQuantity As Double
Public Property Quantity() As Double
Get
Return privateQuantity
End Get
Set(ByVal value As Double)
privateQuantity = value
End Set
End Property
Private privateDiscount As Double
Public Property Discount() As Double
Get
Return privateDiscount
End Get
Set(ByVal value As Double)
privateDiscount = value
End Set
End Property
Private privateExtendedPrice As Double
Public Property ExtendedPrice() As Double
Get
Return privateExtendedPrice
End Get
Set(ByVal value As Double)
privateExtendedPrice = value
End Set
End Property
Private privateFreight As Double
Public Property Freight() As Double
Get
Return privateFreight
End Get
Set(ByVal value As Double)
privateFreight = value
End Set
End Property
End Class
Public Class Address
Private privateCity As String
Public Property City() As String
Get
Return privateCity
End Get
Set(ByVal value As String)
privateCity = value
End Set
End Property
Private privateRegion As String
Public Property Region() As String
Get
Return privateRegion
End Get
Set(ByVal value As String)
privateRegion = value
End Set
End Property
Private privateCountry As String
Public Property Country() As String
Get
Return privateCountry
End Get
Set(ByVal value As String)
privateCountry = value
End Set
End Property
End Class
Public lst As List(Of Invoice) = New List(Of Invoice)()
Private Sub PrepareGridData()
Try
Dim sri As StreamResourceInfo = Application.GetResourceStream(New Uri("IntegratedDemo;component/Examples/Grid/invoices.tab", UriKind.Relative))
Dim sr As New System.IO.StreamReader(sri.Stream)
Dim line As String = Nothing
sr.ReadLine()
line = sr.ReadLine()
Do While line IsNot Nothing
Dim tabs() As String = line.Split(ControlChars.Tab)
lst.Add(New Invoice(tabs(0), tabs(1), tabs(2), tabs(3), tabs(4), tabs(5), Double.Parse(tabs(6)), Double.Parse(tabs(7)), Double.Parse(tabs(8)), Double.Parse(tabs(9)), Double.Parse(tabs(10))))
line = sr.ReadLine()
Loop
sr.Close()
Catch e1 As System.Exception
End Try
End Sub
Note: The 'invoices.tab' file is included in the installation package of VIBlend Controls for Silverlight. The default path to the file is: C:\Program Files\VIBlend\SilverlightControls v.5.0\Source Code\CSharp\Examples\Grid\invoices.tab.