The DataGrid and the “Input string is not in a correct format” message in Silverlight

If you play around with the DataGrid in Silverlight and try some scenarios, maybe you come around the FormatException with the Message “Input String is not in a correct format”. You get this Exception if your Data-Object e.g. has a Property of type int and the user enters some characters in the DataGrid. The Exception doesn’t come up, instead the DataGrid shows it as a validation error. Let’s look at an example. Image you’ve a very simple Person-class containing a FirstName-Property of type string and an Age-Property of type int:
public class Person : INotifyPropertyChanged
{
  private int? _age;
  private string _firstName;
  public string FirstName
  {
    get { return _firstName; }
    set
    {
      _firstName = value;
      Changed("FirstName");
    }
  }

  public int? Age
  {
    get { return _age; }
    set
    {
      _age = value;
      Changed("Age");
    }
  }

  private void Changed(string propertyName)
  {
    if (PropertyChanged != null)
      PropertyChanged(this, 
        new PropertyChangedEventArgs(propertyName));
  }

  public event PropertyChangedEventHandler PropertyChanged;
}
Now let’s fill up a List with some Test-Persons and add that list to the DataContext of your page:
public MainPage()
{
  InitializeComponent();
  this.DataContext = new List { 
    new Person{ FirstName="Thomas",Age=29},
    new Person{ FirstName="Julia",Age=27},
    new Person{ FirstName="Ben",Age=1},
  };
}
No in the XAML-File of your MainPage a DataGrid could be defined like below. Notice the ItemsSource-Property that is bound to the DataContext:
<my:DataGrid ItemsSource="{Binding}"
             AutoGenerateColumns="False">     
  <my:DataGrid.Columns>
    <my:DataGridTextColumn Header="FirstName"
                           Binding="{Binding FirstName}"/>
    <my:DataGridTextColumn Header="Age"
                           Binding="{Binding Age}"/>
  </my:DataGrid.Columns>
</my:DataGrid>
When the User now enters a string as Age, the FormatException is raised before the Property is updated and the Message of the FormatException is displayed in the DataGrid: image Now the question is, where to change this string. The DataGrid has a BindingValidationError-Event, but there you have only read access to the FormatException and the Errormessage. In the Property itself you can’t do anything, because the Exception is thrown before the Age-Property is set. The solution is to define a Property special for Display in the Person-class. Normally you would create a ViewModel that encapsulates the Person-class and contains the additional property. In my case I implement the Property directly in the Person-class. I call it AgeDisplay-Property. Inside that property you can then throw your own FormatException with your special text. My Person-class now looks like this:
public class Person : INotifyPropertyChanged
{
  private int? _age;
  private string _firstName;
  public string FirstName
  {
    get { return _firstName; }
    set
    {
      _firstName = value;
      Changed("FirstName");
    }
  }

  public int? Age
  {
    get { return _age; }
    set
    {
      _age = value;
      Changed("Age");
      Changed("AgeDisplay");
    }
  }


  public string AgeDisplay
  {
    get { return Age.ToString(); ; }
    set
    {
      if (value == null)
      {
        Age = null;
        return;
      }
      int result;
      if (int.TryParse(value, out result))
      {
        Age = result;
      }
      else
      {
        throw new FormatException("Age must be a number. "
                        + " Characters are not allowed.");
      }
    }
  }

  private void Changed(string propertyName)
  {
    if (PropertyChanged != null)
      PropertyChanged(this, 
        new PropertyChangedEventArgs(propertyName));
  }

  public event PropertyChangedEventHandler PropertyChanged;
}
Now you simple bind the DataGrid’s Column to AgeDisplay instead of Age:
<my:DataGrid ItemsSource="{Binding}"
             AutoGenerateColumns="False">     
  <my:DataGrid.Columns>
    <my:DataGridTextColumn Header="FirstName"
                           Binding="{Binding FirstName}"/>
    <my:DataGridTextColumn Header="Age"
                           Binding="{Binding AgeDisplay}"/>
  </my:DataGrid.Columns>
</my:DataGrid>
When the user now enters a string into the Age-Column, the text of the FormatException thrown in the AgeDisplay-Property is displayed: image
Read more...

How-to deploy the Crystal Reports Basic Runtime that’s included in Visual Studio 2008

This week I've to deploy an ASP.NET application containing about 20 reports that have been created with the Crystal Reports Basic Runtime which is included in Visual Studio 2008. (By the way, the application also contains a lot of AJAX-Functionality. It uses the "AJAX Control Toolkit-based" MultiColumnDropDown that I've described in a previous post). When deploying the application to a different machine than your developer-machine, the Crystal Report Basic Runtime must be deployed also. You do that by copying the necessary msi-file to the server and run it. The "necessary" .msi-file is already on your development machine and were installed with Visual Studio 2008. The .msi-files for different plattforms are located in the following paths:
Runtime | MSI-Location
(x86) C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages\CrystalReports10_5\CRRedist2008_x86.msi
(x64) C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages\CrystalReports10_5\CRRedist2008_x64.msi
(IA64) C:\Program Files\Microsoft Visual Studio 9.0\Crystal Reports\CRRedist\IA64\CRRedist2008_ia64.msi
(the table above is contained in MSDN-documentation here) The installation is very simple. You doesn't need to click anything, simply run the .msi and you see the window below. Just wait until it closes. crystalReportsBasic There's also the possibility to include the msi in your setup-application. For more information on that take a look at this thread in MSDN-Forums.
Read more...

VisualStateManager in Silverlight

Last year I've written a german article for the dotnet-magazine about VisualStateManager in Silverlight. As this feature will also be part of WPF 4.0, take a look at the article, which is now available on my homepage. The article is also available on the homepage of Trivadis beside many other articles focused on other IT-Topics. So read and enjoy. :-)
Read more...

The promised code example with the ASP.NET Ajax Multicolumn-Dropdown

During the last months I was asked many times for the code of the Multcolumn-Dropdown that I've described in this post. I've just created a very small sample-application containing such a dropdown. You can download it here. If someone of you creates a control out of that source, I would be glad to get a link to it. :-) Thomas
Read more...

Developing Multicolumn-DropDown/DropDownList with ASP.NET, the GridView and the AJAX Control Toolkit

During the last months I was developing an ASP.NET application and I needed a dropdownlist to display multiple columns in each item. Everyone with a little knowledge in Web-development knows, that HTML doesn't contain built-in support for multicolumn-DropDowns. (more…)
Read more...

TechEd Developers 2007 in retrospect – Monday

Last week I went to Barcelona for five days, participating at TechEd Developers 2007 (05. - 09. november). In the following posts I'll just show you a short summary of my experience at TechEd Developers 2007. Let's start on Monday. On Monday the Keynote-Session started after a life grafiti-show with some really breaking news presented by S. Somasegar (Microsofts Corporate Vice President of the developer division):
  • Visual Studio 2008 and .NET 3.5 will be available to MSDN subscribers by the end of November
  • The first CTP of Microsofts Sync Framework is available now
  • For those of you using Microsoft Popfly, a new Popfly Explorer will be available
  • This year there will already be a CTP of the next Version of Visual Studio, called "Rosario". In 2008 there will be a Beta of it
That are great news, especially the release of Visual Studio 2008 before the end of November. In fact Visual Studio 2008 really differs from the Visual Studio versions we had before. It's the first time you can build applications that target different Framework Versions. With Visual Studio 2008 you can build applications that target .NET 2.0, .NET 3.0 or even .NET 3.5. This feature is called Multi-Targeting, and its possible, because .NET 2.0, .NET 3.0 and .NET 3.5 are all using the same Common Language Runtime, the CLR 2.0 that was introduced with .NET 2.0. Of course another great feature is the possibility to debug into the .NET Framework source code. A great possibility to learn some new best practices and to look how Microsoft has done it The Microsoft Sync Framework is a new platform for enabling roaming, offline and collaboration across your applications, services and devices. The Framework also contains built-in support for synchronizing relational databases, NTFS and FAT file systems, etc. Get some more infos and the first CTP of it here. After the Keynote-Session I joined the Session A Tour of Visual Studio 2008 and the .NET Framework 3.5, speaker Daniel Moth. Daniel explained that the .NET Framework 3.5 is a superset of .NET 3.0 (and so still uses the CLR 2.0). He showed some great features of the new IDE, like the new refactoring features added to the contextmenu in the codeeditor, or the transparency-feature for the intellisense-Window. You can make the intellisense-Window transparent by pressing the Ctrl-Key. So you can look at your code while leaving the intellisense-window open. Daniel also explained the terms of the green and red bits in a really good manner. Green bits are those assemblies that are new in .NET 3.5, and haven't existed in .NET 3.0. Red bits are those assemblies that already existed in .NET 3.0, but are lightly extended and fixed with an installation of .NET 3.5. The Red bits are called "red", because they describe a difficulty. If your .NET 3.0 application takes advantage of a not well implemented feature (maybe call it a bug :-)) in .NET 3.0 and that bug will be fixed with .NET 3.5, your application won't work correctly anymore when you change this bug/feature by installing .NET 3.5. So microsoft has to be very careful with the red bits that are shipped as part of .NET 3.5. From the green and red bits you could also conclude something about the installation of .NET 3.5:
  • If you have .NET 3.0 already installed, an installation of .NET 3.5 will use this existing installation and change the red bit assemblies (some existing .NET 3.0 assemblies) and add some brand new Assemblies (the green bits). That means, after the installation of .NET 3.5 you wont come back to .NET 3.0, but every .NET 3.0 application should run well on .NET 3.5 (if Microsoft has done a good job with the red bits)
  • If you haven't .NET 3.0 installed, an installation of .NET 3.5 will install whole .NET 3.0, plus the red and green bits of .NET 3.5
As the last session on Monday, I joined the session A Windows Communication Foundation (WCF) Walkthrough using Visual Studio 2008, but it wasn't really as amazing as I thought before. The only new stuff for me about WCF I got out of this session were the things that ship with Visual Studio 2008, but nothing about WCF itself. In Visual Studio 2008 there will be new Project-Templates to generate a WCF-Library and a new UI that allows you to test your services easily. In the evening, the "Ask The Experts" was open. The first thing I was looking for was an "Ask The Experts to WPF"-stand. But I didn't find one. There were stands with experts in Visual Studio 2008, SQL Server, BI, Office, Silverlight and Expression and many more, but no stand with experts in WPF. So I just went to the Silverlight stand and asked for the WPF stand. The guy pointed to another person and said: "He asked me exactly the same questions 10 seconds ago, unfortunately there is no stand with WPF-experts". And the other person said, "Some questions about WPF?". Yeah, I thought, here I was right. Looking at his name, I recognized the name of the person and I knew, here I'm really right. It was Ian Griffiths, author of the very first WPF-book, "Programming WPF", currently in its second edition. We spoke about nearly 1 hour about developing real enterprise applications with WPF, about the WPF-Designer in Visual Studio and about using Blend for designing your app. In most cases, Ian and I had the same opinion, and we both are waiting for the datagrid as part of .NET/WPF. :-) Today there are only third-party-grids available from famous control authors like those from Infragistics. Even in .NET 3.5 and WPF 3.5 (it's really called WPF 3.5 and not WPF 1.5), Microsoft introduced no more 2D-Controls. But WPF 3.5 contains some new 3D-Elements like UIElement3D or Viewport2DVisual3D, that receive Input Events and make interactive 3D much easier than in WPF 3.0.
Read more...

The first one

Today I decided to start my own blog. So far I enjoyed life just as a blog-reader for the last couple of years. But now there are several reasons to start my own blog:

  • Up to now I just played around with WPF for nearly two years, and I discovered some things I want to share with you
  • I want to post some technical content to find it later again. Also the provider guarantees me a backup of my site. :-)
  • A blog is a great platform to communicate with you and other guys working with .NET, WPF, XAML, C#, LINQ, ...
  • ...

During the next days I'll provide some stuff just heared and learned from TechEd 2007 in Barcelona.
So stay tuned...

Read more...