Silverlight 4 DataGrid: Load a CSV file

Posted by: Mahesh Sabnis , on 2/11/2011, in Category Silverlight 2, 3, 4 and 5
Views: 44601
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:

CSV File

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.

Share Price class

The following class shown below defines the SharePriceList information for each Company:

Share information collection

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:

Share information manipulation

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:

UserControl_Loaded

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:

Silverlight Read CSV

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.

Silverlight Read CSV DataGrid

The entire source code of this article can be downloaded over here

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

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!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
Mahesh Sabnis is a DotNetCurry author and a Microsoft MVP having over two decades of experience in IT education and development. He is a Microsoft Certified Trainer (MCT) since 2005 and has conducted various Corporate Training programs for .NET Technologies (all versions), and Front-end technologies like Angular and React. Follow him on twitter @maheshdotnet or connect with him on LinkedIn


Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by Calnus Yates on Monday, February 14, 2011 10:18 AM
Excellent article!!!! Will this work on SL3?
Comment posted by Mahesh Sabnis on Monday, February 14, 2011 10:32 PM
Hi Calnus Yates,
Thanks a lot.
This should work in SL3 also.
Thanks
Mahesh Sabnis
Comment posted by JUBAYER AHMED on Wednesday, February 16, 2011 12:02 PM
Thanks... It was really helpful for me...
Comment posted by ramesh on Monday, September 26, 2011 9:34 AM
Thanks for the port, however I got a situation where we need to load the csv file into the grid.. with unknow rows and columns.. I cant create class of that many columns.

please suggest any idea. if possible send some code snippets
Comment posted by David Bell on Saturday, September 22, 2012 2:49 AM
Great tutorial! How would you go about searching for a string within the structure though?