Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to display a message or default row in a Silverlight 3 Datagrid
    text
    copied!<p>I have a DataGrid which is bound to a PagedCollectionView and the underlying collection may contain no items. When this occurs the DataGrid does not render at all, no column headers or anything, and when the DataGrid is then re-bound to another PagedCollectionView that does contain some items it causes a system error</p> <blockquote> <p>System.ArgumentException: Value does not fall within the expected range.<br> at MS.Internal.XcpImports.MethodEx(IntPtr ptr, String name, CValue[] cvData)<br> at MS.Internal.XcpImports.MethodEx(DependencyObject obj, String name) at MS.Internal.XcpImports.UIElement_UpdateLayout(UIElement element)...</p> </blockquote> <p>or </p> <p>Message: Unhandled Error in Silverlight Application Code: 4004<br> Category: ManagedRuntimeError<br> Message: System.ArgumentException: Value does not fall within the expected range.<br> at MS.Internal.XcpImports.CheckHResult(UInt32 hr)<br> at MS.Internal.XcpImports.Collection_InsertValue[T](PresentationFrameworkCollection 1 collection, UInt32 index, CValue value)<br> at MS.Internal.XcpImports.Collection_InsertDependencyObject[T](PresentationFrameworkCollection 1 collection, UInt32 index, DependencyObject value)<br> at System.Windows.PresentationFrameworkCollection 1.InsertDependencyObject(Int32 index, DependencyObject value)<br> at System.Windows.Controls.UIElementCollection.InsertInternal(Int32 index, UIElement value)<br> at System.Windows.PresentationFrameworkCollection 1.Insert(Int32 index, T value)<br> at System.Windows.Controls.DataGrid.InsertDisplayedColumnHeader(DataGridColumn dataGridColumn)<br> at System.Windows.Controls.DataGrid.OnInsertedColumn_PreNotification(DataGridColumn insertedColumn)<br> at System.Windows.Controls.DataGridColumnCollection.InsertItem(Int32 columnIndex, DataGridColumn dataGridColumn)<br> at System.Collections.ObjectModel.Collection 1.Insert(Int32 index, T item)<br> at System.Windows.Controls.DataGridColumnCollection.EnsureRowGrouping(Boolean rowGrouping)<br> at System.Windows.Controls.DataGrid.EnsureRowGroupSpacerColumn()<br> at System.Windows.Controls.DataGrid.RefreshRows(Boolean recycleRows, Boolean clearRows)<br> at System.Windows.Controls.DataGrid.RefreshRowsAndColumns(Boolean clearRows)<br> at System.Windows.Controls.DataGrid.MeasureOverride(Size availableSize)<br> at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Single inWidth, Single inHeight, Single&amp; outWidth, Single&amp; outHeight) </p> <p>Line: 54<br> Char: 13<br> Code: 0 </p> <p>which I assume is caused because the DataGrid is missing a pointer that it should have (but to be honest I really have no idea as I haven't looked into it).</p> <p>The system exception is obviously a problem and I'd like it to not happen. But making the UI look nice when there are no items in the collection is a business requirement and I figure that fixing the UI to display something nice when the collection is empty might just give me a work around for the System Exception.</p> <p>So is it possible to display a message or default row in a Silverlight 3 DataGrid?</p> <p>I've seen <a href="http://forums.silverlight.net/forums/p/23076/81680.aspx" rel="nofollow noreferrer">Jonathan Shen's answer</a> but I was wondering if there was an easier/simpler/built in way now days as his answer pre-dates Silverlight 3. I also have an issue with the View having to create, in the example, a Person collection &amp; object. My Views have no knowledge of the ViewModel so to implement Jonathan's solution I would also have to implement a secondary Person within the View - not the end of the world, but it seems a little hacky.</p> <p>Does anyone have a better solution for displaying something nice when binding a potentially empty collection to a Silverlight DataGrid?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload