Window Support

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Thursday, 16 October 2008

Silverlight 2 Grid : LINQ to SQL + WebService

Posted on 09:34 by Unknown

Last 2 days went like some carnival, lots of mail exchanges and forums post..Silverlight 2 finally came out of shell after wait of last few painful months,but it was fun playing with those RC and Betas.

Silverlight Grid was till the day bit out of focus for me,rather overall Data binding part I was not touching, so I decided its good time now to start doing something good in this arena.

We don’t have DataSet ?, We are not able to get System.Data namespace, I am not good at WCF but I know asmx services, Is WCF is the only way for data binding?

So many questions from last couple of days..here is my answer in bits and pieces which I will be empowering with more post in coming days.

So lets get ready to Bind a simple DataGrid in Silverlight Project.[I assume you know all pre-requisites from Software point of view, if not explore more on http://silverlight.net/GetStarted/ ]

Open a new instance of Silverlight project, it will look like :

SoultionExplorer

Note : With Silverlight 2 now you have “Silverlight.js” file for creating better installed experiences and other customizations.

Step 1 : Add LINQ to SQL

Right click on Web Project and say Add New Item, you will get following window where you need to select LINQ to SQL template and give some name to it.[e.g MyDatabse.dbml]

LINQTOSQL

Now take on table from database,I took Cust from Northwind database.

DBML

Step 2 : Invoke WebService [ asmx one..and not WCF]

We need to add a WebService so as to pass data from LINQ to SQL to Silverlight Grid, one point to be taken note of is that,Silverlight rather data binding in Silverlight is now a days like other applications,more generic oriented and not dependent on old modals like DataSet etc, so all work will now managed by List<Type>.

WebASMX

Code will be like :

        [WebMethod]
        public List<Cust> DBConnect()
        {
            MyDatabaseDataContext db = new MyDatabaseDataContext(); //LINQ to SQL instance
            var temp = from cust in db.Custs
                            where cust.CustName.Length > 0
                            select cust;//LINQ Query
            return temp.ToList(); // Result in form of List<Type>
        }

Step 3 : Add Service Reference & Program the Silverlight Grid

Now your logic is ready which will send a List<Type> with data to your silverlight application, now put a Grid on canvas which will look like :

<data:DataGrid x:Name="MyGrid"
        GridLinesVisibility="Horizontal"
        HeadersVisibility="Column"
        RowBackground="AliceBlue"
        IsReadOnly="True"
        CanUserResizeColumns="False"
        Margin="0,5,0,0">
</data:DataGrid>

and write code to create proxy of that service in your code behind file here it is, Page.xaml.cs and code will be :

public partial class Page : UserControl
   {
       public Page()
       {
           InitializeComponent();
           this.Loaded += new RoutedEventHandler(Page_Loaded);
       }

       void Page_Loaded(object sender, RoutedEventArgs e)
       {
           MyService.DBConSoapClient svrc = new MyService.DBConSoapClient();
           svrc.DBConnectCompleted += new EventHandler<SLDBDemo.MyService.DBConnectCompletedEventArgs>(svrc_DBConnectCompleted);
           svrc.DBConnectAsync();           
       }

       void svrc_DBConnectCompleted(object sender, SLDBDemo.MyService.DBConnectCompletedEventArgs e)
       {
           MyGrid.ItemsSource = e.Result;
       }

}

I hope the code is very straightforward and self explanatory..So now save and just run it !! it will look like in Browser as :

OutPut

So..our first Silverlight 2 Program is ready !! and that too its Data Binding Demo,so now get ready for more demos soon, will back here very soon with more. Thanks !

Vikram.

Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • First Windows Phone 7 update February 2011 - Small update but Big start
    After tons of rumors and set of predictions on Windows Phone 7 all over Internet, Microsoft came up with first Windows Phone 7 minor update ...
  • The little Story of “I Unlock Joy” event by Microsoft and Pune User Group
      This post is about recent “I Unlock Joy” event happened in Pune which was conducted by Microsoft and Pune User Group. Little History : ...
  • Silverlight On Mobile : Windows Phone 7 Splash Screen and Customization
    After talking about 3D capabilities on Windows Phone 7 using Silverlight in last article , Now I am moving ahead with small but equally impo...
  • Silverlight 5 : Platform Invoke (PInvoke) in Silverlight
      Two days back Microsoft announced availability of Silverlight 5 RC,I encourage you to download bits from here , My friend Pete Brown alr...
  • Introduction to Speech Capabilities in Windows Phone 8 – Part 1
    After a long..I am writing blog, I hope and I wish I will resume blogging like I use to in past. Lots of things happened in past few months....
  • MCTS : Microsoft Silverlight 4 Development Exam Guide (70-506) by Packt Publishing
      Hello, After a long time I got chance to come back here.I will soon resume blogging in month of August. Last 4-5 months were horrible due...
  • Introduction to Speech Capabilities in Windows Phone 8 – Part 2
    Hope you enjoyed my last article on Speech Capability in Windows Phone 8, Today I am posting another part or you can say little extension t...
  • Silverlight 3 : Insert & Update Data using WCF Service with DataForm and DataGrid
    In my Lap around Silverlight 3 series, I have written a separate article on DataForm in Silverlight 3, This article is a basic extension to ...
  • Mango : Using DeviceStatus in Windows Phone 7.1
    First of all “Thank You” for your wonderful response and comments on my last article on Silverlight Vs HTML5 ,I hope you like the points I ...
  • Silverlight, HTML5 & Windows 8 : Where we are heading to ?
    This is not the post or yet another post on most happening debate of Silverlight and HTML5, This is just a visit to all of them to realize t...

Blog Archive

  • ►  2013 (4)
    • ►  August (1)
    • ►  April (3)
  • ►  2012 (4)
    • ►  July (1)
    • ►  March (2)
    • ►  January (1)
  • ►  2011 (24)
    • ►  December (1)
    • ►  September (4)
    • ►  August (2)
    • ►  July (1)
    • ►  June (4)
    • ►  May (3)
    • ►  April (3)
    • ►  March (1)
    • ►  February (4)
    • ►  January (1)
  • ►  2010 (21)
    • ►  December (1)
    • ►  November (2)
    • ►  October (3)
    • ►  September (2)
    • ►  August (4)
    • ►  July (5)
    • ►  May (1)
    • ►  April (1)
    • ►  March (1)
    • ►  January (1)
  • ►  2009 (49)
    • ►  December (1)
    • ►  November (5)
    • ►  October (2)
    • ►  September (1)
    • ►  August (5)
    • ►  July (5)
    • ►  June (1)
    • ►  May (5)
    • ►  April (5)
    • ►  March (9)
    • ►  February (4)
    • ►  January (6)
  • ▼  2008 (43)
    • ►  December (3)
    • ►  November (9)
    • ▼  October (7)
      • Silverlight 2 Charts : Building Charts using Silve...
      • Windows Azure : Getting Started with Cloud – Part 1
      • Silverlight 2 : Download Contents on Demand
      • Silverlight “Live” Streaming
      • Silverlight 2 Grid with Silverlight Enabled WCF Se...
      • Silverlight 2 Grid : LINQ to SQL + WebService
      • Silverlight : “Truth” more than “Myth”…
    • ►  September (4)
    • ►  August (2)
    • ►  July (3)
    • ►  June (4)
    • ►  May (3)
    • ►  March (3)
    • ►  February (5)
Powered by Blogger.

About Me

Unknown
View my complete profile