DataGrid Datatable binding error

Nov 3, 2008 at 10:04 AM
Hi, and thanks for releasing the datagrid. I'm currently working on porting my app to the new DataGrid V1, but I have found a problem:

I get an error when I bind to a column name in my datatable

 

 

<toolkit:DataGrid IsSynchronizedWithCurrentItem="True" CanUserSortColumns="True" ItemsSource="{Binding Path=MyDataTable}" AutoGenerateColumns="False" x:Name="listView" Height="Auto" Margin="5,5,5,5" >

 

 

 

<toolkit:DataGridTextColumn Header="Pasient" SortMemberPath="PatientId_FullName" Width="SizeToCells" Binding="{Binding Converter={StaticResource PatientNameTextConverter}, Path=PatientId_FullName}"/>

 

 

 

 

 

<toolkit:DataGridTemplateColumn Header="Status" Width="SizeToCells">

 

 

 

<toolkit:DataGridTemplateColumn.CellTemplate>

 

 

 

<DataTemplate>

 

 

 

<Button Visibility="{Binding Converter={StaticResource PatientIsAdmitAndNotDischargedToVisibleConverter}}" Command="local:WBCommands.PatientSentToStatusCommand" CommandParameter="{Binding}"  Content="{Binding Converter={StaticResource PatientSentToStatusTextConverter}, Path=PatientId_SentToStatus}" Style="{DynamicResource CellButtonStyle}"></Button>

 

 

 

</DataTemplate>

 

 

 

</toolkit:DataGridTemplateColumn.CellTemplate>

 

 

 

</toolkit:DataGridTemplateColumn>

 

Then I get the following errors..

 

 

System.Windows.Data Error: 39 : BindingExpression path error: 'PatientId_FullName' property not found on 'object' ''Object' (HashCode=63926822)'. BindingExpression:Path=PatientId_FullName; DataItem='Object' (HashCode=63926822); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

 

System.Windows.Data Error: 39 : BindingExpression path error: 'PatientId_SentToStatus' property not found on 'object' ''Object' (HashCode=55515938)'. BindingExpression:Path=PatientId_SentToStatus; DataItem='Object' (HashCode=55515938); target element is 'Button' (Name=''); target property is 'Content' (type 'Object')


Note that the datacontext of the datagrid usercontrol is set in the usercontrol constructor:

 

this.DataContext = ViewModel;

 

Where 

 

public DataTable MyDataTable

 

is a INotifyPropertyChanged property on the ViewModel.

Any help on this issue would have been great!

Regards

larsi



2)


Coordinator
Nov 3, 2008 at 12:32 PM
Does setting the DataGrid's ItemsSource Binding Path to MyDataTable.DefaultView help at all?
Nov 3, 2008 at 1:17 PM
No, I got the same errors when binding to the DefaultView of my datatable
Nov 3, 2008 at 1:48 PM

I’ve uploaded a minimal sln at http://cid-5d0909fd6cd506a0.skydrive.live.com/browse.aspx/Offentlig/DataGrid%20Binding%20Error?uc=1 where you can reproduce the problem.

Coordinator
Nov 4, 2008 at 1:32 AM
I tried your sample out and the errors only come from the new row which is by design.  Try setting DataGrid.CanUserAddRows to false and you will notice that you do not see the binding errors anymore.
Nov 4, 2008 at 8:24 AM
Edited Nov 4, 2008 at 8:43 AM
Thanks alot vinsibal! DataGrid.CanUserAddRows=false solved the problem.
Just a quick question about the polling. When I want to reapply the sorting and the filtering after receiving a updated datatable, is this the way to do it:

        // Called from a background thread...
        public void OnUpdatedDataTableReceived(DataTable updatedTable)
        {
            string mySorting = WBData.DefaultView.Sort;
            string myRowfilter = WBData.DefaultView.RowFilter;
            // Update with the new table
            WBData = updatedTable;
            WBData.DefaultView.Sort = mySorting;
            WBData.DefaultView.RowFilter = myRowfilter;
            // My datagrids itemssource is bound to {Binding Path=WBDataView}
            WBDataView = (BindingListCollectionView)CollectionViewSource.GetDefaultView(WBData.DefaultView);
        }

The above code runs fine, but is it safe? Are there some threading issues with updaing the datatable that might show up?