ItemsSource in SfDataGrid

Hi
Im trying to change MS Datagrid to SfDataGrid in my application, but I have problem with setting ItemsSource for this control (in MS it worked).
I need to load data from database and set DataContext for entire Grid to synchronize with textboxes.
Is it possible in that way?
Output window when i run app says:

System.Windows.Data Error: 40 : BindingExpression path error: 'CustomersViewSource' property not found on 'object' ''DataRowView' (HashCode=11766267)'. BindingExpression:Path=CustomersViewSource; DataItem='BindingListCollectionView' (HashCode=20908074); target element is 'SfDataGrid' (Name='CustomerDataGrid'); target property is 'ItemsSource' (type 'Object')

Please help to understand what im doing wrong.

Here is my code:

Class MainWindow

    Dim CustomersViewSource As System.Windows.Data.CollectionViewSource

    Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs) Handles MyBase.Loaded

        Dim NORTHWNDDataSet As WpfApp1.NORTHWNDDataSet = CType(Me.FindResource("NORTHWNDDataSet"), WpfApp1.NORTHWNDDataSet)
        Dim NORTHWNDDataSetCustomersTableAdapter As WpfApp1.NORTHWNDDataSetTableAdapters.CustomersTableAdapter = New WpfApp1.NORTHWNDDataSetTableAdapters.CustomersTableAdapter()
        NORTHWNDDataSetCustomersTableAdapter.Fill(NORTHWNDDataSet.Customers)
        CustomersViewSource = CType(Me.FindResource("CustomersViewSource"), System.Windows.Data.CollectionViewSource)
        CustomersViewSource.View.MoveCurrentToFirst()
    End Sub

End Class

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        xmlns:Syncfusion="http://schemas.syncfusion.com/wpf" x:Class="MainWindow"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <local:NORTHWNDDataSet x:Key="NORTHWNDDataSet"/>
        <CollectionViewSource x:Key="CustomersViewSource" Source="{Binding Customers, Source={StaticResource NORTHWNDDataSet}}"/>
    </Window.Resources>

    <Grid x:Name="Grid1" DataContext="{StaticResource CustomersViewSource}">
            
        <Label Content="Company Name:" HorizontalAlignment="Left" Margin="3" Grid.Row="0" VerticalAlignment="Center"/>
        <TextBox x:Name="CompanyNameTextBox" HorizontalAlignment="Left" Height="22" Margin="138,6,-132,4" Grid.Row="0" Text="{Binding CompanyName, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="120"/>

        <Label Content="Contact Name:" HorizontalAlignment="Left" Margin="3" Grid.Row="1" VerticalAlignment="Center"/>
        <TextBox x:Name="ContactNameTextBox" HorizontalAlignment="Left" Height="22" Margin="138,6,-132,4" Grid.Row="1" Text="{Binding ContactName, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="120"/>
        
        <Syncfusion:SfDataGrid x:Name="CustomerDataGrid" Grid.Row="2" AutoGenerateColumns="True" ItemsSource="{Binding CustomersViewSource}" Grid.ColumnSpan="2" />
        
    </Grid>

</Window>



Attachment: WpfApp1_250dc5f9.zip

5 Replies

GT Gnanasownthari Thirugnanam Syncfusion Team March 20, 2018 03:45 AM UTC

Hi Dariusz, 
  
Now we have modified the given sample to resolve the binding error and populate the items in SfDataGrid based on your requirement, you can download the sample from below mentioned location. 
  
    Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs) Handles MyBase.Loaded 
  
        Dim NORTHWNDDataSet As WpfApp1.NORTHWNDDataSet = CType(Me.FindResource("NORTHWNDDataSet"), WpfApp1.NORTHWNDDataSet) 
        Dim NORTHWNDDataSetCustomersTableAdapter As WpfApp1.NORTHWNDDataSetTableAdapters.CustomersTableAdapter = New WpfApp1.NORTHWNDDataSetTableAdapters.CustomersTableAdapter() 
        NORTHWNDDataSetCustomersTableAdapter.Fill(NORTHWNDDataSet.Customers) 
        CustomersViewSource = CType(Me.FindResource("CustomersViewSource"), System.Windows.Data.CollectionViewSource) 
        CustomersViewSource.View.MoveCurrentToFirst() 
        CustomerDataGrid.ItemsSource = CustomersViewSource.Source 
  
    End Sub 
  
  
Regards, 
Gnanasownthari T. 



DP Dariusz Piech March 20, 2018 07:55 AM UTC

Hi
Thank you for answer, but my problem is to synchronize DataContext for entire window.
Two textboxes above grid have binding set to CollectionViewSource too.
But when i change selected row in datagrid - values in textboxes didnt change.
On the other side - buttons up and down are changing current row in textboxes but not in datagrid.
What i have to do to make it work alltogheter?

Regards
Dariusz Piech


GT Gnanasownthari Thirugnanam Syncfusion Team March 22, 2018 03:43 AM UTC

Hi Dariusz, 

Please find the response for your queries. 

Query 1: Synchronize DataContext for entire window. 
Two textboxes above grid have binding set to CollectionViewSource too. 
But when i change selected row in datagrid - values in textboxes didnt change. 

You can update the TextBox value in UI based on SelectedItem property of SfDataGrid like below code example.  
   <TextBox x:Name="ContactNameTextBox" HorizontalAlignment="Left" Height="22" Margin="138,6,-132,4" Grid.Row="1" Text="{Binding SelectedItem.ContactName,  
            Mode=TwoWay,  
            ElementName=CustomerDataGrid, 
            NotifyOnValidationError=true,  
            ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="120"/> 
Query 2: On the other side - buttons up and down are changing current row in textboxes but not in datagrid. 
In SfDataGrid don’t have direct support to set the SelectedItem while call the CustomersViewSource.View.MoveCurrentToFirst() method but you can achieve your requirement by setting View.CurrentItem to SelectedItem of SfDataGrid like below code example. 
    Private Sub btn_DOWN_Click(sender As Object, e As RoutedEventArgs) Handles btn_DOWN.Click 
        CustomersViewSource.View.MoveCurrentToNext() 
        CustomerDataGrid.SelectedItem = CustomersViewSource.View.CurrentItem 
        index = CustomerDataGrid.ResolveToRowIndex(CustomerDataGrid.SelectedItem) 
        If (index >= 0) Then 
            CustomerDataGrid.ScrollInView(New RowColumnIndex(index, 0)) 
        End If 
    End Sub 



We have modified the sample based on your requirement, you can download it from below mentioned location. 


Please let us know if you need further assistance on this, we will happy to assist you. 

Regards, 
Gnanasownthari T. 



DP Dariusz Piech March 22, 2018 10:28 AM UTC

Hi
Thank You very much!
Now it works the way I need it.
I spent couple of days trying and without your help i would never understand it.
Best Regards
Dariusz


SP Shobika Palani Syncfusion Team March 23, 2018 04:35 AM UTC

Hi Dariusz, 
 
Thank you for your update, 
Please let us know if you need further assistance on this, we will happy to assist you. 
 
Regards, 
Shobika 


Loader.
Up arrow icon