DataGrid Control Beta 2 Changes

 

The DataGrid Beta 2 breaking change list came in a bit too late to make the official documentation. We'll add that for the next doc refresh, but until then, you can see the DataGrid-only breaking changes list here, or the complete, updated list of all breaking changes between Beta 1 and Beta 2 in Word 2007 format here.

 

For DataGrid, most of the changes are API name changes that will require updates to your Beta 1 code. You can fix these by waiting for compiler errors, and then doing a search in the breaking changes list for the broken API names.

 

Additionally, however, there are a few behavior changes, which are harder to catch because they don't show up as compiler errors. These changes currently affect the doc snippet. We'll get that updated for the next doc refresh as well, but until then, the next few sections will describe what you have to do to avoid some problems and take advantage of the cool new autosizing feature.

 

Autogenerated Columns

 

One Beta 2 gotcha is with autogenerated columns. This is a behavior change that manifests as unexpected columns or a blank screen and a run time exception.

 

Here's the relevant bit of the breaking changes list:

 

AutoGenerateColumns behavior modified

...

Beta 2

  • dataGrid.AutoGenerateColumns is true by default
  • ...
  • Column generation is delayed until the DataGrid has loaded ...

 

Because AutoGenerateColumns is now true by default, you must set it to false when you configure columns explicitly, otherwise you'll get both configured and generated columns. This affects the last two demo DataGrid controls in the doc snippet (which you can only see by fixing the next issue first).

 

The delay in column generation affects the first three DataGrid controls in the snippet. Here is an example of the broken code:

 

            // DOESN'T WORK

            dataGrid1.ItemsSource = Customer.GetSampleCustomerList();

            dataGrid1.Columns.RemoveAt(2);           

 

When the RemoveAt method is called, the columns have not yet been autogenerated, so this code throws an ArgumentOutOfRangeException. Here's a quick fix, using a lambda expression to handle the AutoGeneratingColumn event:

 

            dataGrid1.ItemsSource = Customer.GetSampleCustomerList();

            dataGrid1.AutoGeneratingColumn += (s, e) =>

                { if (e.Column.Header.Equals("Address")) e.Cancel = true; };

 

Automatic Sizing

 

Another behavior change that you won't encounter in a compiler error is with automatic sizing. Luckily, this change doesn't break anything. If you have hard-coded column widths in XAML, those values will continue to work. However, you might want to replace them with automatic sizing values in case your cell or header contents change.

 

Many size-related properties, such as the column Width property, now take a DataGridLength structure instead of a double. A DataGridLength structure represents either a double or one of the following automatic-sizing values (which you can specify in XAML):  "SizeToCells", "SizeToHeader", or "Auto". (The "Auto" value means "size to both cells and headers".)

 

See Also

DataGrid Beta 2 Breaking Changes 

Breaking Changes for Silverlight 2 Beta 2 (corrections and additions)

Breaking Changes Between Beta 1 and Beta 2 on MSDN