Welcome to the Xceed Community | Help
Community Search  
More Search Options

DON'T PANIC!

Why There Won't Be A SelectedItems Property And Why You Will Like It

One of the first things you will notice when you`ll start using Xceed DataGrid for Silverlight is the fact that there is no SelectedItem(s) property. That's right. No SelectedItems property. 

The upcoming datagrid for Silverlight is a very different beast than today's current datagrids. It is built from the ground up to load and navigate through remote data sources extremely efficiently. It aims to make those remote data sources feel like they are completely loaded locally in your datagrid, without actually doing so. To achieve this, it takes data virtualization a few steps further than any other datagrid ever has. I talked about some of these advances in my post that introduces Xceed DataGrid for Silverlight. One of them is that everything this datagrid does happens asynchronously. This makes for a highly responsive user experience, but there are a few things developers will have to do differently in order to reap the benefits.

Although it may seem absurd, the decision to not have a SelectedItems property was not taken lightly, and many long discussions were had with proponents on both sides of the debate. So why is it not there? Simple. Because 95% of the time, it wouldn't work. Yes, 95% is an arbitrary number but it is a fair representation. The only way to keep your application responsive, even when the network isn't, is to use asynchronous data virtualization, which means to read and write data in the background. Having a synchronous SelectedItems property in this situation made no sense since the UI would have to wait for the selected items to be returned from the server. So we decided to go with a fully virtualized approach and created a selection model in which "selection ranges" are created. These ranges abstract the need to store the exact list of selected items and provide instantaneous selection across any number of items. If this process were done locally, the selection range would need to be processed as soon as it is created.

For example, let's say you have a large source with and the user selects a few items currently on screen. In this case, the SelectedItems property would return immediately since the items are already loaded. The UI wouldn't lock-up. But if the user selects a group of items and many of them aren't on screen, those would have to be immediately fetched from the data source before SelectedItems could return its value. The UI would freeze for an unpredictable amount of time while those items are returned, making your application feel sluggish.

This is why we exposed the BeginGetSelectedItems and EndGetSelectedItems asynchronous methods rather than a SelectedItem(s) property: we wanted you to be aware that requesting the selected items can be a costly operation and that in most cases, we could not guarantee if and when you would get the result. 

"But I absolutely MUST have a SelectedItems property!" Ok!

 

public IEnumerable<object> SelectedItems
{
   get
   {
      return ( IEnumerable<object> )GetValue( SelectedItemsProperty );
   }
   set
   {
      SetValue( SelectedItemsProperty, value );
   }
}

// Using a DependencyProperty as the backing store for SelectedItems. 
// This enables animation, styling, binding, etc...
public static readonly DependencyProperty SelectedItemsProperty =
DependencyProperty.Register( "SelectedItems", typeof( IEnumerable<object> ), typeof( MainPage ), null );

private void GetSelectedItems()
{
   IAsyncResult result = this.sldgDataGridControl.BeginGetSelectedItems( new AsyncCallback( this.ProcessSelectedItems ), null );

   if( result.IsCompleted )
      this.SelectedItems = this.sldgDataGridControl.EndGetSelectedItems( result );
}

private void ProcessSelectedItems( IAsyncResult result )
{
   if( result.CompletedSynchronously )
      return;

   this.SelectedItems = this.sldgDataGridControl.EndGetSelectedItems( result );
}

private void DataGridControl_SelectionChanged( object sender, EventArgs e )
{
   this.GetSelectedItems();
}

 

Published May 7, 2010 10:54 AM by Jenny [Xceed]

Comments

 

dittrich said:

I understand the reason behind the decision not to create a synchronous selecteditem(s) property.

I'm considering using xceed datdgrid for my application.

In this application, each row in the datagrid has a unique id. Is it possible to at least have the id's of the selected rows stored on the client without requiring a postback?

August 6, 2010 4:51 AM
 

Dhananjaya said:

Hi,

I am doing exactly the same thing but I get this exception. I have been trying to get past this problem for hours and cant find the solution. Can you please help?

The child must always execute synchronously.

  at Xceed.Silverlight.Data.AsyncResultsExtensions.EnsureSyncness[T](T asyncResult)

  at Xceed.Silverlight.Data.EnumeratorExtensions.SyncMoveAndFetch(IAsyncDataSourceEnumerator enumerator, Boolean approximateMove, Int64 expectedMoveCount, Int32 expectedFetchCount, Nullable`1& actualMoveCount, ICollection`1& fetchedItems, RelativeOffset& endPositionOffset, EstimateInt64& totalCount)

  at Xceed.Silverlight.Data.SyncDataSourceEnumerator.MoveAndFetch(Boolean approximateMove, Int64 expectedMoveCount, Int32 expectedFetchCount, Nullable`1& actualMoveCount, ICollection`1& fetchedItems, RelativeOffset& endPositionOffset, EstimateInt64& totalCount)

  at Xceed.Silverlight.Data.EnumeratorExtensions.MoveAndFetch(ISyncDataSourceEnumerator enumerator, Boolean approximateMove, Int64 expectedMoveCount, Int32 expectedFetchCount, ICollection`1& fetchedItems)

  at Xceed.Silverlight.Data.EnumeratorExtensions.MoveAndFetch(ISyncDataSourceEnumerator enumerator, Int64 expectedMoveCount, Int32 expectedFetchCount, ICollection`1& fetchedItems)

  at Xceed.Silverlight.Data.DataSourceEnumeratorWrapper.MoveAndFetch(Int64 expectedMoveCount, Int32 expectedFetchCount, ICollection`1& fetchedItems)

  at Xceed.Silverlight.DataGrid.LayoutManager.MoveHelper(Int64 moveCount)

  at Xceed.Silverlight.DataGrid.LayoutManager.UpdateViewportContent(Boolean pendingTransitionSetup)

  at Xceed.Silverlight.DataGrid.LayoutManager.UpdateViewportContent()

September 23, 2010 9:45 AM
Anonymous comments are disabled
Contact | Site Map | Reviews | Legal Terms of Use | Trademarks | Privacy Statement Copyright 2011 Xceed Software Inc.