Silverlight 4 DataGrid: Load a CSV file
Posted by: Mahesh Sabnis
in Category Silverlight 2, 3, 4 and 5
Abstract: In this article we will see how to load a CSV file (Comma Separated File) into a Silverlight 4 DataGrid control.
I was having a discussion about an application with one of my students who was using Silverlight 4 for business application development. During the discussion, he told about me a scenario in his application, where the data instead of coming from a database using WCF service or for that matter any other service, was instead coming from the File system (or downloaded from the Web Host etc.). His question was how could we read this data and bind it to a Silverlight 4 DataGrid application. This discussion drove me to develop a simple Proof-of-Concept (POC) and present it in the form of an article. I hope you all benefit from it. For this article, I have used a Comma-Separated-Values (.csv) file.
Silverlight provides a mechanism of reading files using System.IO. The file can be added in the Project as an Embedded Resource and then can be read using Assembly class’s GetManifestResourceStream() method (the performance could get slow for large files). The file can be read as a Stream and the data values can be separated, which can be used further for DataBinding. Let’s see how.
Step 1: Open VS 2010 and create a new Silverlight Application. Name it as ‘SL4_Reading_CSV_File’. In the Silverlight project, add a new Text file and name it as ‘ShareInfo.csv’. A snapshot of the file data is as shown below:
This CSV file is included in the source code.
This .csv file contains a Date-wise Share (stocks) information data for various companies. Right-click on this file and change its ‘Build Action’ to ‘Embedded Resource’.
Step 2: In the Silverlight project, add a new class file and name it as ‘DataAccessClassesNew.cs’. This file contains a SharePriceVO struct for storing Share Information like Date and Price.
The following class shown below defines the SharePriceList information for each Company:
Now that we have defined classes related to SharePrices, create a class for Data Access. Defines a method in the class called ‘GetShareData()’. This method will read the .csv file. The code is as shown below:
The above method reads the file and returns the List of ShareInformationCollection class.
Step 3: Open MainPage.xaml file and add the DataGrid, ListBox and TextBlocks in it. Name the DataGrid as ‘dgShare’ and ListBox as ‘listShare’.
Step 4: Open the MainPage.xaml.cs and in the Loaded event write the following code:
The code shown above makes a call to the GetShareData() method in the ShareInformationManipulation class and the List<T> is bound with the ListBox ‘listShare’ present in our xaml .
Step 5: Change the MainPage.xaml file as shown below:
The above XAML code shows that the ItemTemplate property of the ListBox is set to the companyNameTemplate defined in the Resources section. The DataGrid is bound with the result returned from the ‘SelectedItem’ object from the ListBox. The TextBlock ‘txtCompanyName’ is bound with CompanyName which is returned as a SelectedItem object of the ListBox.
Step 6: Run the application and select the Company Name in the ListBox. The DataGrid then shows the Share information for the selected company name.
The entire source code of this article can be downloaded over here
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!