Silverlight 3 DataGrid Columns Grouping using PagedCollectionView

In this post I am showing how to implement Column Grouping in SIlverlight 3 DataGrid,

Before starting make sure you have SIlverlight3_Tools installed in your system, not SIlverlight 3 Beta, as there are lost of changes in Grouping of Columns from SIlverlight 3 Beta to Silverlight 3 RTW.

image

You can follow the few simple steps below to get the Grouping for your Silverlight 3 DataGrid.

Else you can also download the code demonstrated from here.

1. Create an Silverlight Application using you Visual Studio 2008 IDE, and add a hosting web application in the project.

2. Once you have done with this, you will get an two projects in your Solution, you need to code only in your silverlight project.

3. Now add a C# class file called Person.cs in your Silverlight Project. This class is used to provide some sample data to Silverlight DataGrid, you can change this to your other datasources like SQL, XML, etc.

I have added the following lines of code in Person.cs

.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

   1: public class Person
   2:     {
   3:         public string FirstName { get; set; }
   4:         public string LastName { get; set; }
   5:         public string City { get; set; }
   6:         public string Country { get; set; }
   7:         public int Age { get; set; }
   8:  
   9:         public List<Person> GetPersons()
  10:         {
  11:             List<Person> persons = new List<Person>
  12:             {
  13:                 new Person
  14:                 { 
  15:                     Age=32, 
  16:                     City="Bangalore", 
  17:                     Country="India", 
  18:                     FirstName="Brij", 
  19:                     LastName="Mohan"
  20:                 },
  21:                 new Person
  22:                 { 
  23:                     Age=32, 
  24:                     City="Bangalore", 
  25:                     Country="India", 
  26:                     FirstName="Arun", 
  27:                     LastName="Dayal"
  28:                 },
  29:                 new Person
  30:                 { 
  31:                     Age=38, 
  32:                     City="Bangalore", 
  33:                     Country="India", 
  34:                     FirstName="Dave", 
  35:                     LastName="Marchant"
  36:                 },
  37:                 new Person
  38:                 { 
  39:                     Age=38,
  40:                     City="Northampton",
  41:                     Country="United Kingdom", 
  42:                     FirstName="Henryk", 
  43:                     LastName="S"
  44:                 },
  45:                 new Person
  46:                 { 
  47:                     Age=40, 
  48:                     City="Northampton", 
  49:                     Country="United Kingdom", 
  50:                     FirstName="Alton", 
  51:                     LastName="B"
  52:                 },
  53:                 new Person
  54:                 { 
  55:                     Age=28, 
  56:                     City="Birmingham",
  57:                     Country="United Kingdom",
  58:                     FirstName="Anup", 
  59:                     LastName="J"
  60:                 },
  61:                 new Person
  62:                 { 
  63:                     Age=27,
  64:                     City="Jamshedpur",
  65:                     Country="India", 
  66:                     FirstName="Sunita", 
  67:                     LastName="Mohan"
  68:                 },
  69:                 new Person
  70:                 { 
  71:                     Age=2, 
  72:                     City="Bangalore", 
  73:                     Country="India", 
  74:                     FirstName="Shristi", 
  75:                     LastName="Dayal"
  76:                 }
  77:             };
  78:  
  79:             return persons;
  80:         }
  81:     }

4. Now since my data is ready, I will add the DataGrid control in my XAML page, to make the presentation more attractive, I have added few more lines of code.

5. I have also added a ComboBox Control to Select the Grouping Columns name.

My XAML code will look something like this below.

.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

   1: <UserControl 
   2:     xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"  
   3:     x:Class="Silverlight3DataGrid.MainPage"
   4:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   5:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   6:     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
   7:              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
   8:     mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
   9:     <Grid x:Name="LayoutRoot">
  10:         <Grid.RowDefinitions>
  11:             <RowDefinition Height="0.154*"/>
  12:             <RowDefinition Height="0.483*"/>
  13:             <RowDefinition Height="0.362*"/>
  14:         </Grid.RowDefinitions>
  15:  
  16:         <StackPanel Orientation="Horizontal">
  17:             <TextBlock Text="Select Sort Criteria" 
  18:                        VerticalAlignment="Center" />
  19:  
  20:             <TextBlock Text="    " />
  21:             
  22:             <ComboBox Grid.Row="0" 
  23:                       HorizontalAlignment="Left" 
  24:                       Width="200" 
  25:                       Height="30" x:Name="SortCombo" 
  26:                       SelectionChanged="SortCombo_SelectionChanged">
  27:                 
  28:                 <ComboBoxItem Content="Country" ></ComboBoxItem>
  29:                 <ComboBoxItem Content="City" ></ComboBoxItem>
  30:                 <ComboBoxItem Content="Age" ></ComboBoxItem>
  31:             </ComboBox>
  32:         </StackPanel>
  33:         
  34:         <data:DataGrid x:Name="PersonGrid" Grid.Row="1"></data:DataGrid>
  35:  
  36:     </Grid>
  37: </UserControl>

6. Now as my Data and Presentation is ready, its time for me to write some lines of code to Retrieve my sample data, group them into columns and then Bind it to the Grid.

Please find below the rest of the Code which demonstrates how I have Grouped the Columns using PagedCollectionView and PropertyGroupDescription.

   1: public partial class MainPage : UserControl
   2:     {
   3:         PagedCollectionView collection;
   4:  
   5:         public MainPage()
   6:         {
   7:             InitializeComponent();
   8:             BindGrid();
   9:         }
  10:  
  11:         private void BindGrid()
  12:         {
  13:             Person person = new Person();
  14:             PersonGrid.ItemsSource = null;
  15:             List<Person> persons = person.GetPersons();
  16:             collection = new PagedCollectionView(persons);
  17:             collection.GroupDescriptions.Add(new 
  18:                 PropertyGroupDescription("Country"));
  19:  
  20:             PersonGrid.ItemsSource = collection;
  21:         }
  22:  
  23:         private void SortCombo_SelectionChanged(object sender, 
  24:             SelectionChangedEventArgs e)
  25:         {
  26:             ComboBoxItem person = SortCombo.SelectedItem as ComboBoxItem;
  27:             collection.GroupDescriptions.Clear();
  28:             collection.GroupDescriptions.Add(new 
  29:                 PropertyGroupDescription(person.Content.ToString()));
  30:  
  31:             PersonGrid.ItemsSource = null;
  32:             PersonGrid.ItemsSource = collection;
  33:         }
  34:     }

.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

Yes its that simple, and its done. I know I have not given much description here, because nothing much to explain here. In the code above I am loading the Grid with my sample data coming from my Person class and default grouping the Person with Country.

Later on SortCombo_SelectionChanged, I am dynamically fetching the Selected column names from the ComboBox and Sorting on that.

Once you will run this application the screen will look something like this as given below.

Grouped by Country

image

Grouped by City

 image 

 Grouped by Age

image

 

You can group according to your requirement, like Grouping Active and Deleted items, etc.

 

You can also download the sample code from here.

 

Cheers

~Brij

FREE Cheat Sheets for Developers(Quick Reference Card)

Hi All, here is the link for the great library of cheat sheets, I assure you that after looking into this collection you will be amazed....these Cheat Sheets are...

  • Written by bestselling authors and leading experts
  • Reliable information on major developer topics
  • Filled with useful tips and source code examples
  • PDF looks great on-screen or printed from your printer

To name a few...

  • Design Patterns
  • Silverlight 2, I am eagerly waiting for Silverlight 3, I know its too early...
  • C#
  • Core .NET
  • Core ASP.NET
  • Scrum
  • Windows PowerShell

These are just few of my interest, but you can always register and logon to download lot more of your interest too...from the link below.

http://refcardz.dzone.com/

Posted by bmdayal | 2 comment(s)

Everything you need to get started...MOSS 2007, WSS 3.0

This is a hub for SharePoint 2007 and WSS 3.0 Resources with two advantages: All resources are hand-picked and vetted for quality, and each topic contains a with hand-tuned search designed to return the latest content which you can then filter further to find what you need.

Eli's SharePoint 2007 Resources

Posted by bmdayal | with no comments

Sharing source code between .NET and Silverlight

A common problem when developing Silverlight applications is how to share classes and in particular (Entity Framework) entities which are compiled for the full .NET framework in a Silverlight application. Silverlight is a browser plugin and a platform independent subset of the full .NET framework. Therefore Visual Studio does not allow referencing .NET assemblies from your Silverlight application.

Fortunately there is a very simple technique to share and reuse source code. Full details of which you can find in the link below

http://www.scip.be/index.php?Page=ArticlesNET28&Lang=EN

In this article Stefan Cruysberghs demonstrated this technique with several real world examples and I will give you some handy tips.

This linked has helped me, as I have SOA, where WCF Services, DataContracts, ServiceContracts are shared between my ASP.NET , WPF and Silverlight application.

Migration from Silverlight 2.0 to Silverlight 3

As currently I am working on Migration from Silverlight 2.0 to Silverlight 3, so I thought to share or bookmark few of the important links which I am going through.

Find below the links

http://msdn.microsoft.com/en-us/library/cc645049(VS.95).aspx

Silverlight 3 Released! What is new/changed?

 

The following list encompasses all changes made between the Silverlight Toolkit March 2009 release and the Silverlight Toolkit July 2009 release.

Silverlight Toolkit July 2009 changes

 

The following list encompasses all change made between the Silverlight Toolkit December 2008 release and the Silverlight Toolkit March 2009 release.

Silverlight Toolkit March 2009 change list.

 

In the process of upgrade there are few more challenges I am going to face

Upgrade of my WCF Services to implement Silverlight 3 Validation controls, as my DataContract is residing in Service Layer, and I am using WSSF to generate the WCF DataContract, ServiceContract, etc. As all the DataCOntracts are AutoGenerated so implementing the Validation in Silverlight will be bit tricky.

Since I am still in the process of upgrade, so if you too have some good links which will help me to upgrade my Services as I mentioned above then do share here.

 

Thanks

Brij Mohan

SharePoint 2010 Sneak Peak

I guess this will be blogged extensively the coming hours and days: Microsoft has released official documentation about the next version of SharePoint.

Read all about it here:

http://sharepoint.microsoft.com/2010/Sneak_Peek/Pages/default.aspx.

  • General overview
    SharePoint 2010 enables organizations to connect and empower people through an integrated set of rich features. Get a sneak peek of SharePoint 2010 product features today.

  • For IT Professionals

    For IT professionals, SharePoint 2010 offers enhancement to drive productivity, scalable unified infrastructure, and flexible deployment. Learn more about how to cut IT costs with SharePoint 2010.

  • For Developers
    For developers, SharePoint 2010 provides the business collaboration platform to rapidly build solutions and respond to business needs. Check out SharePoint 2010 features sneak peek for developers.

Posted by bmdayal | with no comments

What's New in Silverlight 3?

Silverlight 3 has been officially released on 10th July 2009 and available for download!

Speaking of which, here are some of the key features of Silverlight 3.

Out-of-Browser Support

Users can run a Silverlight 3 application in the browser or run it directly from the desktop even when they're not connected to the Internet. This supports several sync scenarios that can be useful when a connection isn't always available.

Enhanced Graphics Support

New features include GPU acceleration, perspective 3-D support, bitmap and pixel APIs for dynamically generating images, videos, etc. Animations can also be eased in and out and perform many other cool effects. Pixel shaders allow objects to have different effects applied to them, like shadows and blurs.

New Controls

Silverlight 3 provides many new controls that can be used to build solid line-of-business (LOB) applications. Controls can be bound to each other using element-to-element binding, and validation can also be performed more easily. Several new controls are also available in the Silverlight 3 toolkit released by Microsoft.

Better Navigation

Navigating between Silverlight "pages" is now built-in along with better search engine optimization (SEO) support and deep linking.

Enhanced Text Rendering

One knock against Silverlight 2 was that text didn't render as clearly as it should in some situations. Silverlight 3 includes a major update to the text-rendering engine. Text renders very crisply now.

Enhanced Styles

Silverlight 3 provides merged dictionary support, allowing multiple style files to be merged into an application (similar to how a standard Web application can use multiple CSS files). This allows themes and other styles to be switched much more easily. Styles can also be based on other styles, similar to inheritance in OO languages.

Faster Transfer of Data

Windows Communication Foundation (WCF) support was available in Silverlight 2 but Silverlight 3 now adds support for binary XML serialization, which allows data to be transferred between a Silverlight application and a WCF service much faster than before.

Assembly Caching

Silverlight 3 allows developers to store assemblies on a central company server and Silverlight 3 applications can then download them as needed rather than downloading everything upfront in a single .XAP file. This can significantly speed-up application load times.

Enhanced Networking Support

A new client networking (ClientHttp) stack is available that supports more verbs than simply GET/POST. Applications that fully leverage REST APIs will benefit from this new feature.

HD Media Support

Silverlight 3 includes support for GPU acceleration (as mentioned earlier) as well as 1080p HD videos to be played over the Web. New codec support for H.264, AAC audio and MPEG-4 content is also included. If you need to provide media solutions, then Silverlight covers all of the major scenarios now.

Here are some other changes to Visual Studio and Silverlight applications in general:

  • The Visual Studio 2008 designer has been removed for Silverlight 3 applications. A lot of developers turned it off, anyway, so all of the dev efforts have gone into the designer that'll be available in Visual Studio 2010. Expression Blend 3 can also be used, of course, or you can also use Kaxaml.
  • The Silverlight ASP.NET server control isn't used now. It simply emitted the object and associated params tags, anyway.
  • Expression Blend 3 has also been released, along with a new feature called SketchFlow. SketchFlow allows application prototypes and mock-ups to be created more easily in order to get customer feedback. Though I rarely used Blend for coding previously, it now includes code intellisense, which is a nice feature to have and adds support for behaviors, importing Adobe Photoshop and Illustrator files, and integrating sample data into applications.

To get started with Silverlight 3, go here.

You can find some more useful links in my previous post.

Posted by bmdayal | with no comments

Silverlight Presentation on 17th July 2009

1 Layouts Presentation.zip

2 Introducing_Silverlight_2.zip

3 Introductionto Silverlight 3.zip

4 Silverlight Control Lifecycle.zip

These are the Slides of the Silverlight Presentation which I had given on 17th July 2009, other useful links where you can download the examples and Installers are given below.

WHAT’S NEW IN SILVERLIGHT 3?

Download Microsoft® Silverlight™ 3 Tools for Visual Studio 2008 SP1

This download will install the following:

  • Silverlight 3 developer runtime
  • Silverlight 3 software development kit
  • KB967143 for Visual Studio 2008 SP1
    and/or
    KB967144 for Visual Web Developer 2008 Express with SP1
  • Silverlight 3 Tools for Visual Studio 2008 SP1
    and/or
    Silverlight 3 Tools for Visual Web Developer 2008 Express with SP1

The Official Site of Silverlight | Microsoft Silverlight

Microsoft Expression Blend 3

Silverlight Community

Microsoft Silverlight 3 Offline Documentation

And for all other things,

http://www.silverlight.net

And for those who are mostly involved in Silverlight Designing

http://www.kirupa.com/blend_silverlight/index.htm

http://expression.microsoft.com/en-us/cc184874.aspx

There are few basic concepts which I have used in my Presentation like Automatic Properties, Object Initializers, LINQ, etc you can find almost all these in Scott’s Blog

 

Thanks

~Brij

Cryptographic failure while signing assembly ... Access is denied.

I received the following error today while deploying the Silverlight WebPart in IIS.

Cryptographic failure while signing assembly 'MyWebpartAssemblyName.dll'. Access is denied.

Seems I had a permissions error. Did a bit of searching on Google and found this post on a blog by R. Aaron Zupancic. That did the trick.

I would have never thought that the permissions error had something to do with the C:\Documents and Settings\All Users\Application Data\Microsoft\Crypto\RSA\MachineKeys folder.

Silverlight 2.0 DataGrid Demo Application with Formatting of Data

In this post I am going to show you the basic example of Creating the Page with DataBound Silverlight 2.0 DataGrid, and also I will show you how to format the Data Using IValueConverter in DataGrid dynamically.

Create a new project and Select Silverlight Application.

image

Name your application and select Ok, select the options as given in the screen below.

image

Once you select Ok, your solution should look something similar to the one given in the screen below.

clip_image002[9]

Select the DataGrid from the ToolBox and Drag and Drop it on you page, you can also go and Add the DataGrid manually in you XAML code, but if you are doing so then you may also have to add the reference to the assembly and define the custom tag to use for your DataGrid. This is done automatically when you do the Drag and Drop of the control.

clip_image002[11]

Now if you are aware of the Silverlight Layouts then you can format your page and position your DataGrid in more better way as given in the Code below, in the code below I am defining the Rows and Adding the Header for the Page and in the next row I am placing my DataGrid.

Page.xaml

   1: <UserControl xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"  x:Class="DataGridDemo.Page"
   2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
   4:     xmlns:conv="clr-namespace:DataGridDemo"
   5:     Width="600" Height="300">
   6:     
   7:     <Grid x:Name="LayoutRoot" Background="White" HorizontalAlignment="Center">
   8:         <Grid.RowDefinitions>
   9:             <RowDefinition Height="50"/>
  10:             <RowDefinition Height="*"/>
  11:         </Grid.RowDefinitions>
  12:         <TextBlock Text="List of Player and their details" FontSize="20" Grid.Row="0"></TextBlock>
  13:         <data:DataGrid x:Name="GridPlayer" Grid.Row="1">
  14:             
  15:         </data:DataGrid>
  16:     </Grid>
  17: </UserControl>

 

Now my next task is to create the sample Data to bind the DataGrid, if you already have some data source then you can your that only, or you can use the one as given in the code below. I am creating the Player.cs and PlayerCollection.cs class, which will have to List of Players. Then in GetDemoData in Player.cs I am populating the demo data.

Player.cs

   1: using System;
   2: using System.Net;
   3: using System.Windows;
   4: using System.Windows.Controls;
   5: using System.Windows.Documents;
   6: using System.Windows.Ink;
   7: using System.Windows.Input;
   8: using System.Windows.Media;
   9: using System.Windows.Media.Animation;
  10: using System.Windows.Shapes;
  11:  
  12: namespace DataGridDemo
  13: {
  14:     public class Player
  15:     {
  16:         public string FirstName { get; set; }
  17:         public string LastName { get; set; }
  18:         public string SkillSet { get; set; }
  19:         public DateTime DOB { get; set; }
  20:  
  21:         public static PlayerCollection GetDemoPlayers()
  22:         {
  23:             PlayerCollection players = new PlayerCollection 
  24:             {
  25:                 new Player 
  26:                 { 
  27:                     FirstName = "Brij", 
  28:                     LastName = "Mohan", 
  29:                     SkillSet = "Rugby",
  30:                     DOB=Convert.ToDateTime("08/31/1977")
  31:                 }, 
  32:                 new Player
  33:                 {
  34:                     FirstName="David", 
  35:                     LastName="Osborne", 
  36:                     SkillSet="Cricket",
  37:                     DOB=Convert.ToDateTime("01/11/1976")
  38:                 },
  39:                 new Player
  40:                 {
  41:                     FirstName="Henry", 
  42:                     LastName="Smith", 
  43:                     SkillSet="Chess",
  44:                     DOB=Convert.ToDateTime("11/10/1981")
  45:                 }
  46:             };
  47:  
  48:             return players;
  49:         }
  50:     }
  51:  
  52: }

PlayerCollection.cs

   1: using System;
   2: using System.Net;
   3: using System.Windows;
   4: using System.Windows.Controls;
   5: using System.Windows.Documents;
   6: using System.Windows.Ink;
   7: using System.Windows.Input;
   8: using System.Windows.Media;
   9: using System.Windows.Media.Animation;
  10: using System.Windows.Shapes;
  11: using System.Collections.Generic;
  12:  
  13: namespace DataGridDemo
  14: {
  15:     public class PlayerCollection: List<Player>
  16:     {
  17:  
  18:     }
  19: }

 

Now I have all the data ready to bind the Silverlight DataGrid, so I will modify my Page.xaml.cs as the one given below. In the code below I am just binding the Silverlight DataGrid using the ItemSource property of the DataGrid.

Page.xaml.cs

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Net;
   5: using System.Windows;
   6: using System.Windows.Controls;
   7: using System.Windows.Documents;
   8: using System.Windows.Input;
   9: using System.Windows.Media;
  10: using System.Windows.Media.Animation;
  11: using System.Windows.Shapes;
  12:  
  13: namespace DataGridDemo
  14: {
  15:     public partial class Page : UserControl
  16:     {
  17:         public Page()
  18:         {
  19:             InitializeComponent();
  20:             BindDataGrid();
  21:         }
  22:         private void BindDataGrid()
  23:         { 
  24:             GridPlayer.ItemsSource = Player.GetDemoPlayers();
  25:         }
  26:     }
  27: }

Now you can run the application and can see the output.

image

Now one thing you can notice in the screen below, i.e the DOB column, the dates are not coming what we are expecting and also the column name is also not very Readable, so we need to define the ColumnTemplates and add few more codes to customize our data and have more control on the Desing.

image

To format the DOB column, first I have Created the class, I named it here in my project as DateConverter.cs, and also added the reference to System.Windows.Data

Now all we need to derive the Class from IValueConverter Interface and implement the interface, like the one given below

 

DateConverter.cs

   1: using System;
   2: using System.Net;
   3: using System.Windows;
   4: using System.Windows.Controls;
   5: using System.Windows.Documents;
   6: using System.Windows.Ink;
   7: using System.Windows.Input;
   8: using System.Windows.Media;
   9: using System.Windows.Media.Animation;
  10: using System.Windows.Shapes;
  11: using System.Windows.Data;
  12: using System.Globalization;
  13:  
  14: namespace DataGridDemo
  15: {
  16:     public class DateConverter : IValueConverter
  17:     {
  18:  
  19:         #region IValueConverter Members
  20:  
  21:         /// <summary>
  22:         /// Converts values on their way to the UI for display
  23:         /// </summary>
  24:         /// <param name="value">The value to be formatted</param>
  25:         /// <param name="targetType">The target type of the conversion</param>
  26:         /// <param name="parameter">A format string to be used in the 
  27:         /// formatting of the value</param>
  28:         /// <param name="culture">The culture to use for formatting</param>
  29:         /// <returns>The converted or formatted object</returns>
  30:         public object Convert(object value, Type targetType, object parameter, 
  31:             System.Globalization.CultureInfo culture)
  32:         {
  33:             if (parameter != null)
  34:             {
  35:                 string formatterString = parameter.ToString();
  36:                 if (!string.IsNullOrEmpty(formatterString))
  37:                 {
  38:                     return string.Format(culture, formatterString, value);
  39:                 }
  40:             }
  41:             return value.ToString();
  42:         }
  43:  
  44:         /// <summary>
  45:         /// Converts values on their way back from the UI to the backend.
  46:         /// </summary>
  47:         /// <param name="value">The value to be formatted</param>
  48:         /// <param name="targetType">The target type of the conversion</param>
  49:         /// <param name="parameter">A format string to be used in the 
  50:         /// formatting of the value</param>
  51:         /// <param name="culture">The culture to use for formatting</param>
  52:         /// <returns>The converted or formatted object</returns>
  53:         public object ConvertBack(object value, Type targetType, object parameter, 
  54:             System.Globalization.CultureInfo culture)
  55:         {
  56:             return System.Convert.ToDateTime(value, CultureInfo.CurrentUICulture);
  57:         }
  58:  
  59:         #endregion
  60:     }
  61: }

Next you need to Use template and set the following property in your DataGrid

   1: AutoGenerateColumns="False"

Run the application now, you will not see anything in your default.aspx, because we have set the AutoGenerateColumns as False, so we have to write few bits of code to define our own Column Template for the DataGrid, I personally would prefer using the DataGrid.Column Template, as this gives me more control over the data and the design.

Add the following code inside your DataGrid

   1: <data:DataGrid.Columns>
   2:                 <!--First Name-->
   3:                 <data:DataGridTemplateColumn Header="First Name">
   4:                     <data:DataGridTemplateColumn.CellTemplate>
   5:                         <DataTemplate>
   6:                             <TextBlock Text="{Binding FirstName, Mode=TwoWay}"/>
   7:                         </DataTemplate>
   8:                     </data:DataGridTemplateColumn.CellTemplate>
   9:                 </data:DataGridTemplateColumn>
  10:                 <!--End First Name-->
  11:                 <!--Last Name-->
  12:                 <data:DataGridTemplateColumn Header="Last Name">
  13:                     <data:DataGridTemplateColumn.CellTemplate>
  14:                         <DataTemplate>
  15:                             <TextBlock Text="{Binding LastName, Mode=TwoWay}"/>
  16:                         </DataTemplate>
  17:                     </data:DataGridTemplateColumn.CellTemplate>
  18:                 </data:DataGridTemplateColumn>
  19:                 <!--End Last Name-->
  20:                 <!--SkillSet-->
  21:                 <data:DataGridTemplateColumn Header="Skill Set">
  22:                     <data:DataGridTemplateColumn.CellTemplate>
  23:                         <DataTemplate>
  24:                             <TextBlock Text="{Binding SkillSet, Mode=TwoWay}"/>
  25:                         </DataTemplate>
  26:                     </data:DataGridTemplateColumn.CellTemplate>
  27:                 </data:DataGridTemplateColumn>
  28:                 <!--End SkillSet-->
  29:                 <!--DOB-->
  30:                 <data:DataGridTemplateColumn Header="Date of Birth">
  31:                     <data:DataGridTemplateColumn.CellTemplate>
  32:                         <DataTemplate>
  33:                             <TextBlock Text="{Binding DOB, Mode=TwoWay}"/>
  34:                         </DataTemplate>
  35:                     </data:DataGridTemplateColumn.CellTemplate>
  36:                 </data:DataGridTemplateColumn>
  37:                 <!--End DOB-->
  38:             </data:DataGrid.Columns>

Run the application.

You can see the same output, except the Header text of DOB, you had when you had the AutoGenerateColumns=True,

Now I am going to show how you can use your DateConverter.cs class to format your DateTime, to do so

First you need to import the class which we are going to use to format the DateTime, and also we have to define the tag, this can be done by adding the following code in your UserControl Tag

   1: xmlns:conv="clr-namespace:DataGridDemo"

 

The completed UserControl Tag will look something like this as given below

   1: <UserControl xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"  x:Class="DataGridDemo.Page"
   2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
   4:     xmlns:conv="clr-namespace:DataGridDemo"
   5:     Width="600" Height="300">

Now add the following code in your XAML code just above your LayoutRoot Grid Layout

   1: <UserControl.Resources>
   2:         <conv:DateConverter x:Key="FormatConverter" />
   3: </UserControl.Resources>

Now you have all the required classes and tag defined for your DateConverter.cs, we have to specify the Column in which we are going to apply this format, as currently we are formatting the DateTime, so we have to apply this for our DOB column, by modifying the following code, as given below

 

   1: <TextBlock Text="{Binding DOB, Mode=TwoWay, Converter={StaticResource FormatConverter}, 
   2: ConverterParameter=\{0:D\}}"/>

 

Now run your application you will get the following output

Final Screen

In the screen above you can notice the Date of Birth date format,

So these are the very basic example I had given here to use the Silverlight 2.0 DataGrid and format the data in the Silverlight 2.0 DataGrid.

You can be more creative and can design the whole new look itself and can get the more control on the data using Silverlight 2.0 DataGrid control.

You can download the source code of the complete solution from here

My Crush List of 2009

These are few things which currently I am working on and some technologies which I want to learn in coming months to master these technologies I need your help,

So please post some good tutorials and articles on any of the following topics, if you have any.

1. Silverlight 2.0/3.0

2. Microsoft .NET 2008/2010 and Framework 3.5 SP1/4.0

3. Model View Presenter and Model View Controller Architecture

4. WCF, WSSF incuding Creating custom templates for Implementation Technologies, etc

5. Visual Studio SDK

6. Visual Studio Database Projects (SQl Server 2008 and SQL Server 2005)

7. SQL Server 2008 new Features (Auditing, Change Data Capture and Change Data Tracking)

There are some links which already I am following is given below:

Automatic properties and Object initializers
http://weblogs.asp.net/scottgu/archive/2007/03/08/new-c-orcas-language-features-automatic-properties-object-initializers-and-collection-initializers.aspx

LINQ Tutorials
http://weblogs.asp.net/scottgu/archive/2006/05/14/446412.aspx

Lambda Expression
http://weblogs.asp.net/scottgu/archive/2007/04/08/new-orcas-language-feature-lambda-expressions.aspx

WSSF Home page
http://www.codeplex.com/servicefactory

WSSF Hands on Lab
http://www.codeplex.com/Release/ProjectReleases.aspx?ProjectName=servicefactory&ReleaseId=7846

Thanks
Brij Mohan

SQL Server 2008 Auditing, Change Data Capture and Tracking

When I started working on SQL Server 2008, I was truly amazed with the new features of SQL Server 2008, one of that cool features is auditing in SQL Server 2008. First I thought to give here step by step Walkthrough, but I thought instead of reinventing the wheel, why don't I just give you some really good links only, which will guide you in much better way and also save some of my time :)


Please find below the links which I followed and found very useful.

Introduction and Step by Step Walkthrough
http://blogs.msdn.com/manisblog/archive/2008/07/21/sql-server-2008-auditing.aspx

MSDN Link
http://msdn.microsoft.com/en-us/library/dd392015.aspx

But again I would like to mention that Auditing is the feature which will give you the Audit in Object, Schema and Database Level So my next challenge was to Keep track of the Database at the Field/Column level which Auditing does not provide (Correct me if I am wrong),

So I found one new features of SQL Server 2008 known as Change Data Capture/ Change Data Tracking, which is again a very cool feature,

Change data capture enables SQL Server administrators and developers to capture insert, update and delete events in a sql server table as well as the details of the event which caused data change on the relevant database table.

When you apply Change Data Capture feature on a database table, a mirror of the tracked table is createad which reflects the same column structure of the original table and additional columns that include metadata which is used to summarize what is the change in the database table row.

So enabling the Change Data Capture feature on a database table, you can track the activity on modified rows or records in the related table.

Change Data Capture (CDC) can be considered as Microsoft solution for data capture systems in SQL Server 2008 and next versions.

There were samples of data capture solutions implemented for Microsoft SQL Server 2000 and SQL Server 2005 by using after update/insert or
after delete triggers. But CDC enables SQL Server developers to build sql server data archiving without a necessity to create triggers on
tables for logging. SQL Server database administrators or programmers can also easily monitor the activity for the logged tabled.

You can find more details of how to configure and enable the Change Data Source on Tables
http://www.databasejournal.com/features/mssql/article.php/3720361/Microsoft-SQL-Server-2008----Change-Data-Capture--Part-I.htm

Difference Between Change Data Capture and Change Data Tracking
http://msdn.microsoft.com/en-us/library/cc280519.aspx

MSDN Link
http://msdn.microsoft.com/en-us/library/bb522489.aspx

I hope you will find these useful.

~Brij

Posted by bmdayal | 6 comment(s)

Problem Starting SQL Server Analysis Services.

For those who are facing problem in Starting SQL Server Analysis Services, please follow the following steps to fix the issue.

  1. Open Control Panel
  2. Go to Regional and Language Options Locale Settings
  3. Change the Format, Location and Administrative from English(India) to English(United States)
  4. Restart the System
  5. Once your system is Restarted open the Registry Editor (from Start = > Run => regedit)
  6. Navigate to HKEY_USERS
  7. Look for LSA entry @ S-1-5-18
  8. Search for LocaleName
  9. Update the following keys
    1. Locale = 00000409
    2. LocaleName = en-US
    3. sCountry = United States

    10. Open the SQL Server Configuration Manager, and start the SQL Server Analysis Services.

FYI: This is caused because Windows Vista is supporting en-IN whereas SQL Server 2005 does not provide support for en-IN, so if you try to run SQL Server Analysis Services it will give the following exception (can be found in Event Viewer)

The service cannot be started: Message-handling subsystem: The message manager for the default locale cannot be found. The locale will be changed to US English. Errors in the metadata manager. LOG file extension can be only .LOG. Message-handling subsystem: The message manager for the default locale cannot be found. The locale will be changed to US English. Message-handling subsystem: The message manager for the 16393 locale cannot be found. Internal error: Failed to generate a hash string.

Fixing Intellisense in Silverlight XAML Code

For those who are facing the problems of intellisense not working for XAML files, please follow the steps below to fix that.

 

1. Uninstall the Silverlight using the control panel snap in as given in the screenshot below.

clip_image001

2. Download and Install the Silverlight Tools by clicking on the following location

http://www.microsoft.com/downloads/details.aspx?FamilyID=c22d6a7b-546f-4407-8ef6-d60c8ee221ed&displaylang=en

3. If you have installed Silverlight DataGrid 2008 Update, then re-installing the Silverlight Tools will delete all your files from your C:\Program Files\Microsoft SDKs\Silverlight\v2.0\Libraries\Client , so you will loose the DataGrid December 2008 update. So you will need to fix this again, details of which you can find in the below given link.

http://silverlight.net/forums/p/59990/176470.aspx

 

FYI: This usually happens when Silverlight Toolkit is installed before the Visual Studio 2008 IDE, Usually when we install Silverlight Toolkit, it installs the patch for Visual Studio 2008 IDE which enables intellisense. But in absence of VS2008 IDE, Silverlight Toolkit skips this patch.

This patch for intellisense will get installed only when it detects VS2008 IDE already installed in our Systems.

 

I hope this helps.

Cheers

~Brij

More Posts Next page »