Defining Silverlight DataGrid Columns at Runtime


This post has been updated to work with the RTW version of the Silverlight 2 DataGrid.  The code examples are not guaranteed to work with previous Beta versions of the DataGrid. Read more about the features that the Silverlight 2 DataGrid has to offer…

Now that you know the basics of the Silverlight DataGrid and how to specify the Columns in XAML, you might want to customize your DataGrid’s columns at runtime.

This process is pretty straight forward, so instead of doing the usual end-to-end walk through, I’m going to provide you with a Rosetta Stone between the static XAML form and the dynamic C#/VB form for the scenario in the last post.

Defining a DataGrid

For any of these columns to be useful you are going to first need a DataGrid to add them to.  The following creates a DataGrid, adds it as a child of the root layout Grid, and sets its ItemsSource to a collection called "source".

C#

DataGrid targetDataGrid = new DataGrid();
targetDataGrid.ItemsSource = source;
targetDataGrid.AutoGenerateColumns = false;
LayoutRoot.Children.Add(targetDataGrid);

VB

Dim TargetDataGrid As New DataGrid
TargetDataGrid.ItemsSource = Source
TargetDataGrid.AutoGenerateColumns = False
LayoutRoot.Children.Add(TargetDataGrid)

 

Defining a DataGrid Text Column

The following creates a DataGridTextColumn bound to the FirstName property with a header of "First Name", and adds it to the columns collection of the DataGrid named "targetDataGrid".

Static
 <data:DataGrid x:Name="targetDataGrid">
     <data:DataGrid.Columns>
         <data:DataGridTextColumn Header="First Name" 
             Binding="{Binding FirstName}" />
     </data:DataGrid.Columns>
 </data:DataGrid>

Dynamic

C#

using System.Windows.Data;
...
DataGridTextColumn textColumn = new DataGridTextColumn();
textColumn.Header = "First Name";
textColumn.Binding = new Binding("FirstName");
targetDataGrid.Columns.Add(textColumn);

VB

Imports System.Windows.Data
...

Dim TextColumn As New DataGridTextColumn
TextColumn.Header = "First Name"
TextColumn.Binding = New Binding("FirstName")
TargetDataGrid.Columns.Add(TextColumn)

 

Defining a DataGrid CheckBox Column

The following creates a DataGridCheckColumn bound to the Available property with a header of "Available", and adds it to the columns collection of the DataGrid named "targetDataGrid".

Static
 <data:DataGrid x:Name="targetDataGrid">
     <data:DataGrid.Columns>
         <data:DataGridCheckBoxColumn Header="Available " 
             Binding="{Binding Available}" />
     </data:DataGrid.Columns>
 </data:DataGrid>

Dynamic

C#

using System.Windows.Data;
...
DataGridCheckBoxColumn checkBoxColumn = new DataGridCheckBoxColumn();
checkBoxColumn.Header = "Available";
checkBoxColumn.Binding = new Binding("Available");
targetDataGrid.Columns.Add(checkBoxColumn);

VB

Imports System.Windows.Data
...

Dim CheckBoxColumn As New DataGridCheckBoxColumn
CheckBoxColumn.Header = "Available"
CheckBoxColumn.Binding = New Binding("Available")
TargetDataGrid.Columns.Add(CheckBoxColumn)

 

Defining a DataGrid Template Column

The following creates a DataGridTemplateColumn bound to the Birthday property with a header of "Birthday", and adds it to the columns collection of the DataGrid named "targetDataGrid".

Static
<UserControl.Resources>
    <local:DateTimeConverter x:Key="DateConverter" />
</UserControl.Resources>
...
<data:DataGrid x:Name="targetDataGrid" AutoGenerateColumns="False" >
    <data:DataGrid.Columns>
        <data:DataGridTemplateColumn Header="Birthday">
            <data:DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock 
                        Text="{Binding Birthday, 
                        Converter={StaticResource DateConverter}}" 
                        Margin="4"/>
                </DataTemplate>
            </data:DataGridTemplateColumn.CellTemplate>
            <data:DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <basics:DatePicker 
                        SelectedDate="{Binding Birthday, Mode=TwoWay}" />
                </DataTemplate>
            </data:DataGridTemplateColumn.CellEditingTemplate>
        </data:DataGridTemplateColumn>
    </data:DataGrid.Columns>
</data:DataGrid>

Dynamic

There are two ways to dynamically create a template column for a DataGrid.  One is to load in the CellTemplate and CellEditingTemplates as DataTemplates from resources, and the other is to construct the DataTemplates on the fly using XamlReader.

1. Resources Method

This method creates the CellTemplate DataTemplate and the CellEditingTemplate DataTemplate in XAML and stores them as named resources.  Then when the column is created the DataTemplates are used.

Use the below XAML to create the DataTemplates as resources to support the code for this method.

<UserControl.Resources>
    <local:DateTimeConverter x:Key="DateConverter" />

    <DataTemplate x:Key="myCellTemplate">
        <TextBlock 
            Text="{Binding Birthday, 
            Converter={StaticResource DateConverter}}" 
            Margin="4"/>
    </DataTemplate>

    <DataTemplate x:Key="myCellEditingTemplate">
        <basics:DatePicker 
            SelectedDate="{Binding Birthday, Mode=TwoWay}" />
    </DataTemplate>
</UserControl.Resources>

C#

using System.Windows.Data;
...
DataGridTemplateColumn templateColumn = new DataGridTemplateColumn();
templateColumn.Header = "Birthday";
templateColumn.CellTemplate = (DataTemplate)Resources["myCellTemplate"];
templateColumn.CellEditingTemplate = 
                       (DataTemplate)Resources["myCellEditingTemplate"];
targetDataGrid.Columns.Add(templateColumn);

VB

Imports System.Windows.Data
...

Dim TemplateColumn As New DataGridTemplateColumn
TemplateColumn.Header = "Birthday"
TemplateColumn.CellTemplate = Resources("myCellTemplate")
TemplateColumn.CellEditingTemplate = Resources("myCellEditingTemplate")
TargetDataGrid.Columns.Add(TemplateColumn)

2. XamlReader Method

This method creates the DataTemplate inline using the XamlReader class.  This class takes a string and parses it to try to build a visual tree.  In this case we are creating DataTemplates.  This method is especially useful if the DataTemplate itself has to be dynamic.  One example being if you wanted to modify what the element in the template was bound to.

Warning: This method is considerably more difficult than the resources method.  I recommend only using this if you need to dynamically create the DataTemplate.

Some things to watch out for:

  1. You will need to declare any XAML namespace that is used in the data template
  2. Any custom XAML namespace needs to specify both the clr-namespace and the assembly
  3. You cannot have white space between the xmlns: and the name of your namespace
  4. External resources cannot be referenced, they need to be declared inline
  5. The entire template is a single line, so if you get a XAML Parse exception, it will always say line 1, however the character is fairly accurate if you were to concatenate all of your lines.
  6. When using the StringBuilder approach shown below, make sure that you have the correct white space at the end of a line so that it is correctly separated when concatenated with the next line.

Now that the warnings are out of the way, here is how you do the equivalent of the code above:

C#

using System.Windows.Data;
using System.Windows.Markup;
using System.Text;
...

DataGridTemplateColumn templateColumn = new DataGridTemplateColumn();
templateColumn.Header = "Birthday";

StringBuilder CellTemp = new StringBuilder();
CellTemp.Append("<DataTemplate ");
CellTemp.Append("xmlns='http://schemas.microsoft.com/winfx/");
CellTemp.Append("2006/xaml/presentation' ");
CellTemp.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' ");

//Be sure to replace "YourNamespace" and "YourAssembly" with your app's 
//actual namespace and assembly here
CellTemp.Append("xmlns:local = 'clr-namespace:YourNamespace");
CellTemp.Append(";assembly=YourAssembly'>");

CellTemp.Append("<Grid>");
CellTemp.Append("<Grid.Resources>");
CellTemp.Append("<local:DateTimeConverter x:Key='DateConverter' />");
CellTemp.Append("</Grid.Resources>");
CellTemp.Append("<TextBlock ");
CellTemp.Append("Text = '{Binding Birthday, ");
CellTemp.Append("Converter={StaticResource DateConverter}}' ");
CellTemp.Append("Margin='4'/>");
CellTemp.Append("</Grid>");
CellTemp.Append("</DataTemplate>");

StringBuilder CellETemp = new StringBuilder();
CellETemp.Append("<DataTemplate ");
CellETemp.Append("xmlns='http://schemas.microsoft.com/winfx/");
CellETemp.Append("2006/xaml/presentation' ");
CellETemp.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' ");
CellETemp.Append("xmlns:basics='clr-namespace:System.Windows.Controls;");
CellETemp.Append("assembly=System.Windows.Controls' >");
CellETemp.Append("<basics:DatePicker ");
CellETemp.Append("SelectedDate='{Binding Birthday, Mode=TwoWay}' />");
CellETemp.Append("</DataTemplate>");

templateColumn.CellTemplate =
    (DataTemplate)XamlReader.Load(CellTemp.ToString());
templateColumn.CellEditingTemplate =
    (DataTemplate)XamlReader.Load(CellETemp.ToString());
targetDataGrid.Columns.Add(templateColumn);

VB

Imports System.Windows.Data
Imports System.Text
Imports System.Windows.Markup
...

Dim TemplateColumn As New DataGridTemplateColumn
TemplateColumn.Header = "Birthday"

Dim CellTemp As New StringBuilder
CellTemp.Append("<DataTemplate ")
CellTemp.Append("xmlns='http://schemas.microsoft.com/winfx/")
CellTemp.Append("2006/xaml/presentation' ")
CellTemp.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' ")

'Be sure to replace "YourNamespace" and "YourAssembly" with your app's 
'actual namespace and assembly here
CellTemp.Append("xmlns:local = 'clr-namespace:YourNamespace")
CellTemp.Append(";assembly=YourAssembly'>")

CellTemp.Append("<Grid>")
CellTemp.Append("<Grid.Resources>")
CellTemp.Append("<local:DateTimeConverter x:Key='DateConverter' />")
CellTemp.Append("</Grid.Resources>")
CellTemp.Append("<TextBlock ")
CellTemp.Append("Text = '{Binding Birthday, ")
CellTemp.Append("Converter={StaticResource DateConverter}}' ")
CellTemp.Append("Margin='4'/>")
CellTemp.Append("</Grid>")
CellTemp.Append("</DataTemplate>")

Dim CellETemp As New StringBuilder
CellETemp.Append("<DataTemplate ")
CellETemp.Append("xmlns='http://schemas.microsoft.com/winfx/")
CellETemp.Append("2006/xaml/presentation' ")
CellETemp.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' ")
CellETemp.Append("xmlns:basics='clr-namespace:System.Windows.Controls;")
CellETemp.Append("assembly=System.Windows.Controls' >")
CellETemp.Append("<basics:DatePicker ")
CellETemp.Append("SelectedDate='{Binding Birthday, Mode=TwoWay}' />")
CellETemp.Append("</DataTemplate>")

TemplateColumn.CellTemplate = XamlReader.Load(CellTemp.ToString())
TemplateColumn.CellEditingTemplate = XamlReader.Load(CellETemp.ToString())
targetDataGrid.Columns.Add(TemplateColumn)
Comments (46)

  1. Rui Marinho says:

    Hi there Scott, i m m trying to create de datagrid dynam but it dosen’t show anything.. here is my code:

        DataGrid dataGrid = new DataGrid();

               dataGrid.GridlinesVisibility = DataGridGridlines.Horizontal;

               dataGrid.HeadersVisibility = DataGridHeaders.Column;

               dataGrid.ColumnWidth=85;

               dataGrid.RowHeight=30;

               if (operacao == "bomb")

               {

                   //criar colunas dinamicas

                   DataGridTextBoxColumn textBoxColumn = new DataGridTextBoxColumn();

                   //criar header com sort

                   HyperlinkButton hyperbut = new HyperlinkButton();

                   hyperbut.Content = "Nº Int";

                   hyperbut.Tag = "nint";

                   hyperbut.Click += new RoutedEventHandler(Sort_Click);

                   hyperbut.TextDecorations = TextDecorations.Underline;

                   textBoxColumn.Header = hyperbut;

                   textBoxColumn.DisplayMemberBinding = new Binding("nint");

                   dataGrid.Columns.Add(textBoxColumn);

                   DataGridTextBoxColumn textBoxColumn1 = new DataGridTextBoxColumn();

                   //criar header com sort

                   HyperlinkButton hyperbut1 = new HyperlinkButton();

                   hyperbut1.Content = "Nome";

                   hyperbut1.Tag = "nome";

                   hyperbut1.Click += new RoutedEventHandler(Sort_Click);

                   hyperbut1.TextDecorations = TextDecorations.Underline;

                   textBoxColumn1.Header = hyperbut;

                   textBoxColumn1.DisplayMemberBinding = new Binding("nome");

                   dataGrid.Columns.Add(textBoxColumn1);

                   DataGridTextBoxColumn textBoxColumn2 = new DataGridTextBoxColumn();

                   //criar header com sort

                   HyperlinkButton hyperbut2 = new HyperlinkButton();

                   hyperbut2.Content = "Quadro";

                   hyperbut2.Tag = "quadro";

                   hyperbut2.Click += new RoutedEventHandler(Sort_Click);

                   hyperbut2.TextDecorations = TextDecorations.Underline;

                   textBoxColumn2.Header = hyperbut;

                   textBoxColumn2.DisplayMemberBinding = new Binding("quadro");

                   dataGrid.Columns.Add(textBoxColumn2);

  2. scmorris says:

    Hi Rui,

    There are two things that could be missing from your code:

    1) You are not setting the DataGrid’s ItemsSource

    2) You are not adding the DataGrid to the visual tree

    I’ll update the post to show how to add a DataGrid itself.  Let me know if you are doing both those things and still running into trouble.

    Thanks,

    Scott

  3. Corey Gaudin on The Next Web, Matt Casto&#39;s Scrolling Textbox, Martin Mihaylov with an Image Control

  4. Here is the latest in my link-listing series .&#160; Also check out my ASP.NET Tips, Tricks and Tutorials

  5. Here is the latest in my link-listing series .&#160; Also check out my ASP.NET Tips, Tricks and Tutorials

  6. Here is the latest in my link-listing series .&#160; Also check out my ASP.NET Tips, Tricks and Tutorials

  7. 【原文地址】 April 28th Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Silverlight 【原文发表日期】 Monday, April 28, 2008

  8. Hier vind je mijn laatste link-lijst series . Bekijk ook mijn ASP.NET Tips, Tricks and Tutorials pagina

  9. Aqui está a última versão da minha série de listas de links . Verifique também minha página de Dicas

  10. Jordy says:

    Is there a way to add a row in the datagrid? Something like this?

    targetDataGrid.Rows.Add(templateRow)

  11. Jordy says:

    Is it also possible to add a DataRow in code behind?

    For the Colums you can use: dataGridName.Columns.Add(columnName);

    But there is no: dataGridName.Rows.Add(rowName);  You can define a datagridrow ( DataGridRow rowName = new DataGridRow() ), but than you can’t add it to the dataGrid.

  12. scmorris says:

    Hi Jordy,

    Unfortunately all of the rows in the Silverlight DataGrid need to have an equivalent item in the collection that it is bound to, and the "unbound" behavior that you are requesting is not supported currently in the DataGrid.  Depending on what you want to accomplish with the new rows there are a couple of paths you could take including adding proxies in your collection, or retemplating the DataGrid to have a row frozen at the bottom used for something like a totals row.

    Hope this helps.

    – Scott

  13. Here is the latest in my link-listing series .&#160; Also check out my ASP.NET Tips, Tricks and Tutorials

  14. Alphatester001 says:

    Hi,

    with the xamlReader how do you add an event handler? – if you define it within the stringBuilder, the xamlReader throw an exception. I tried to define it through a style in a ResourceDictionary that I merge later on. If I look for style entry in the resourceDictionary it is there, same for the dataTemplate,but I get an error when the control get rendered. the error is : Cannot find resource named ‘{mystyle}’. Resource names are case sensitive.  Error at object ‘System.Windows.Controls.ComboBox’ . I double checked in the template I referred to mystyle and same  thing in my second dictionary.So error message is inappropriate. It is more an issue to locate the style in the dictionary… any clue?

  15. rtr0mdrn says:

    Scott, I would like to know how to add a column dynamically after all autogenerated columns have been created.  In other words I would like for the grid to generate columns using the binding context, and after this has completed insert a more static or functional column (such as a button) which can be used to interact with the bound data or add additional features.  I see the autogenerating column event but there does not appear to be any eventing related to binding having completed or all column generation completed.  Is it possible to mix autogeneration with static column insertion and if so how best to do this at runtime?  Thanks.

  16. johnzabroski says:

    Is it okay to specify a ConverterParameter using XML literals in the C# code behind?  I basically copied your XML literal example verbatim, but added a ConverterParameter and instead of the source value being a property, it was a whole collection with the ConverterParameter used as a key to pick an item in the collection.  Sort of like LINQifying David Anson’s IValueConverter SwissArmyKnife PropertyViewer.

  17. johnzabroski says:

    Scott,

    I am not sure your code here is 100% correct.  You show how to dynamically construct XAML using a CLR StringBuilder and then pass that into XamlReader.Load(string).  Do XML fragments have to be fully-qualified using <?Mapping XmlNamespace="blah" ClrNamespace="clr-blah-app" Assembly="blah-assembly" ?> ?

    I keep getting ManagedRuntime 4004 exceptions when I try to use your approach above.

  18. (以下内容全部整理自博客堂Scottgu博客中文版)Silverlight技巧,诀窍,教程和链接 【原文地址】SilverlightTips,Tricks,…

  19. SLNewb says:

    The columns part of this is real handy.  

    For the resources method, it would be helpful to me for the example to include the following:

    – reference necessary for dateconverter (or class def), can’t seem to find this in my project.

    – xmlns definition required

    – example of locating this resource template outside the control.xaml (which seems to be the most likely usage, yes?)

  20. oneone says:

    is possible to add a listbox ?

  21. As you might have heard , we just released Silverlight 2 , and with it the first version of the Silverlight

  22. Ronnie Meijer says:

    I have a simple datagrid and I want to align one column to the right.

    Is there a way to do this dynamically.

  23. marjolein says:

    How can I bind a two-dimensional object? So I don’t know in advance how many columns there are. Each columns contains a set of values. The itemSource of my datagrid is the collection of columns, but what is then the binding of my value in the columns?

          Column1  Column2 …

    Item 1    0        6

    Item 2    1        8

    Item 3    5        0

  24. Hema says:

    To display a cell having multiple columns at runtime i would like to have a datagridcolumn on cell click or a datagridcomboboxcolumn which can display multiple columns at runtime

  25. Andrus says:

    How to add event handler using XamlReader.Load method ?

    Code below causes exception AG_E_PARSER_BAD_PROPERTY_VALUE

    I tried to move AddToCart_Click method to Page class but problem persists.

    Or is it possible to set click event handler in code ?

    Andrus.

    Page.xaml:

    <UserControl xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"  x:Class="DataGridDynamicColClickHandler.Page"

       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation”>http://schemas.microsoft.com/winfx/2006/xaml/presentation"”>http://schemas.microsoft.com/winfx/2006/xaml/presentation”>http://schemas.microsoft.com/winfx/2006/xaml/presentation"

       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"”>http://schemas.microsoft.com/winfx/2006/xaml"

      xmlns:myGrid="clr-namespace:DataGridDynamicColClickHandler"

    >

       <Grid x:Name="LayoutRoot" Background="White">

           <myGrid:DataGridBase x:Name="InventoryList"></myGrid:DataGridBase >

       </Grid>

    </UserControl>

    Page.xaml.cs:

    using System.Windows;

    using System.Windows.Controls;

    using System.Windows.Markup;

    namespace DataGridDynamicColClickHandler

    {

       public partial class Page : UserControl

       {

           public Page()

           {

               InitializeComponent();

               InventoryList.Create();

           }

       }

       public class Product

       {

           public string Name { get; set; }

       }

       public class DataGridBase : DataGrid

       {

           public void Create()

           {

               ItemsSource = new Product[] { new Product { Name = "Apple" }};

               DataGridTemplateColumn xtemplateColumn = new DataGridTemplateColumn();

               xtemplateColumn.CellTemplate = XamlReader.Load(string.Format(@"<DataTemplate

    xmlns=’http://schemas.microsoft.com/winfx/2006/xaml/presentation”>http://schemas.microsoft.com/winfx/2006/xaml/presentation‘

    xmlns:x=’http://schemas.microsoft.com/winfx/2006/xaml

    xmlns:myGrid=""clr-namespace:DataGridDynamicColClickHandler""

    >

    <Button Click=""AddToCart_Click"" />

    </DataTemplate>"

    )) as DataTemplate;

               Columns.Add(xtemplateColumn);

           }

           public void AddToCart_Click(object sender, RoutedEventArgs e)

           {

           }

        }

    }

  26. Jason Young says:

    Hi Scott, any idea how I can get a frozen column on the right hand side of the grid?

  27. Beto says:

    Hi, when i use the below XAML to create the DataTemplates as resources to support the code for this method.

    <UserControl.Resources>

       <local:DateTimeConverter x:Key="DateConverter" />

       <DataTemplate x:Key="myCellTemplate">

           <TextBlock

               Text="{Binding Birthday,

               Converter={StaticResource DateConverter}}"

               Margin="4"/>

       </DataTemplate>

    Blend close in same time !!!!!

    just paste de follow xaml :  

    Converter={StaticResource DateConverter}}"

    Makes the Blend close , a messageBox show a internal error . please help!!!

    sorry my bad inglish!

    Thanks

  28. John says:

    Has anyone added a summary /totals to a templated data grid (other than devexpress) … or to a plain grid?  

    Did you use grid/cell events to trigger recalculation once changes were made to the underlying observable collection?

  29. Rick Kierner says:

    Good post.  Very helpful.  It’s working for me the way you’ve explained it.  I’m creating dynamic data columns just fine.  My problem is when I want to handle the click event or text changed event of one of my columns.  If I use a static column template, the event will fire.  If I use a dynamic template, I get a parse error identifying the Click="MyClickEventHandler" section of code.  Ideas?

  30. Tony says:

    Hi Scott;

    How can some access control in datagrid ?

  31. Servando says:

    I’m using Silverlight 2.0 and trying the XamlReader Method.

    I’m doing a basic sample with just one column, added stackpanel to the page and a datagrid with name TestGrid to the panel.

    My project’s name is DBGridTest, the class’ name is the default (Page).

    I changed your code and instead of YourNamespace putted "DBGridTest" which is the namespace for my project

    and instead of YourAssembly putted "DBGridTest.Page".

    I have a class with Birthday property of DateTime type, just that property for now.

    I create a generic List of theis class and add 5 elements to it.

    Then in the Page constructor after InitializeComponent() y put the following:

               TestGrid.Columns.Add(templateColumn);

               TestGrid.AutoGenerateColumns = false;

               TestGrid.ItemsSource = employeelist;

    When I set the Datagrid height small so that only the headers are shown it runs ok.

    But if I change the height so that the column’s content will would be shown the iexplore.exe throws and exception 5828.

    when instead of adding the columns I set AutoGenerateColumns = true; it all runs ok.

    Am I missing a property on the DataGrid or something else?

  32. Kamran says:

    Thanks for the great post. I’ve referred to it several times 🙂

  33. Parimaladevi says:

    hi,

    the post is very helpful. i want to know

    How to add event handler using XamlReader.Load method ?.Please let me know . this is very urgent for me

    Thanks

  34. scmorris says:

    Hi Parimaladevi,

    This thread should help you do what you are trying to do: http://forums.silverlight.net/forums/t/102290.aspx

    Hope this helps.

    -Scott

  35. Rakesh says:

    Hi Scott,

     I am trying to add data in the last row of a datagrid. When the user clicks the add button, I want to show textbox in the last row of the grid so that the user can enter information in it and Upon saving I am using ADo .NET Data services. I save using the savechanges.batch options. Can you please help me to show how to add a dynamic textbox in last column of the grid.

  36. this is very nice explaination,this very help full when go with dynamic tamplate

    thank you it very help fuul to me

  37. Vikas Vyas says:

    Great Artical

  38. Sridhar says:

    thank you verymuch. it helps me a lot.

  39. mkamoski says:

    Awesome article!!! Thanks.

  40. I have been using the RIA Services Silverlight 4 wizard to manipulate my data… Is the MVVM methodology better?

  41. Hey Scott!

    I'm trying to add columns dynamically as you have outlined but for some reason I don't get to see the column headers or the columns themselves! The DataGrid is set to AutoGenerateColumns to false and I'm creating my columns in the constructor. The PagedCollectionView I pass to the grid, from my list, shows the columns when AutoGenerateColumns is true or when the columns are defined in the XAML, does that mean I have to set the columns in an specific stage of creation of the grid? I'm using SIlverlight 4. Please help me, I've been stuck in this for a few days now.

    Thanks in advanced!

  42. scmorris says:

    WaterDamageRestoration,

    It depends what you want out of your project.  RIA Services is great at getting a project up and running quckly while still giving it room to scale.  MVVM is all about future compatibility.  

    If you think that you are likely to change the view, or have multiple views, then MVVM might be worth the investment.  Also, they aren't mutually exclusive as MVVM was taken into consideration when RIA services was being designed.

    humble.apprentice

    Without seeing the code, not sure exactly what is going wrong.  

    Just be sure that you are adding the columns to the correct DataGrid:

    targetDataGrid.Columns.Add(checkBoxColumn);

    And that the DataGrid has been added to the visual tree:

    LayoutRoot.Children.Add(targetDataGrid);

    Hope this helps!

    -Scott

  43. Hey Scott!

    Thanks for the reply! I have posted my question here: forums.silverlight.net/…/230813.aspx. The grid.Columns.Add method is called inside a foreach passing the CreateTemplateForGrid method. My grid is actually in the XAML of the MainPage, it isn't being added at runtime. Let me know if I can give you more information.

    Thanks again!

  44. how can i bind password box in datagrid.

    when it display in grid like password char and when edit that time it will be plan text.

    how can i do it.

    thank you in advance