CBreeze
CBreeze

Reputation: 2965

DataGrid Not Binding Correctly

I have one page with a Tabcontrol that contains two DataGrids. I am able to bind the first one without a hitch however the second will not receive any data no mattter what I try. I feel like it may be due to something to do with the DataContext, however I am still unsure.

This is how I bind my first DataGrid;

    private async void FillDataGrid()
    {
        var Companies = new ObservableCollection<CompanyModel>();
        var waitWindow = new PleaseWait();

        waitWindow.Show();
        Companies = await ReturnCompanies();
        CompanyICollectionView = CollectionViewSource.GetDefaultView(Companies);
        DataContext = this;

        dataGrid.SelectedIndex = 0;
        waitWindow.Close();
    }

The DataGrid itself looks like this;

<DataGrid x:Name="dataGrid" ColumnWidth="*" IsReadOnly="true" 
                      Margin="10" FontSize="14" 
                      HeadersVisibility="Column" AutoGenerateColumns="False" 
                      CanUserAddRows="False" SelectionChanged="DataGridSelectionChanged"
                      IsSynchronizedWithCurrentItem="True"
                      EnableRowVirtualization="True"
                      ItemsSource="{Binding CompanyICollectionView}"
                      MinColumnWidth="0">                       
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Company Name" Binding="{Binding Name}"/>
                        <DataGridTextColumn Header="Town" Binding="{Binding Town}"/>
                        <DataGridTextColumn Header="Post Code" Binding="{Binding Postcode}"/>
                    </DataGrid.Columns>
                </DataGrid>

My second DataGrid is bound in a very similar way;

    private async void FillContactsDataGrid(object sender, MouseButtonEventArgs e)
    {
        var Contacts = new ObservableCollection<ContactsModel>();
        var waitWindow = new PleaseWait();

        waitWindow.Show();
        Contacts = await ReturnContacts();
        ContactsICollectionView = CollectionViewSource.GetDefaultView(Contacts);
        DataContext = this;

        contactsDataGrid.SelectedIndex = 0;
        waitWindow.Close();
    }

The second DataGrid;

<DataGrid x:Name="contactsDataGrid" ColumnWidth="*" IsReadOnly="true" 
                      Margin="10" FontSize="14" 
                      HeadersVisibility="Column" AutoGenerateColumns="False" 
                      CanUserAddRows="False"
                      IsSynchronizedWithCurrentItem="True"
                      EnableRowVirtualization="True"
                      ItemsSource="{Binding ContactsICollectionView}"
                      MinColumnWidth="0"
                      Grid.Row="1" Grid.ColumnSpan="2">
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Date" Binding="{Binding ContactDate, StringFormat=\{dd-MM-yyyy\}}"/>
                        <DataGridTextColumn Header="To Person" Binding="{Binding PersonName}"/>
                        <DataGridTextColumn Header="Contract" Binding="{Binding Contract}"/>
                        <DataGridTextColumn Header="Type" Binding="{Binding TypeOfContact}"/>
                        <DataGridTextColumn Header="Made By" Binding="{Binding ContactMadeBy}"/>
                        <DataGridTextColumn Header="Contract Summary" Binding="{Binding ContactDescription}"/>
                    </DataGrid.Columns>
                </DataGrid>

I have looped through the ContactsICollectionView and printed out details of the contacts and had no issue, so I know that the collections are being filled correctly, it's just the binding of the DataGrids where I'm going wrong.

Upvotes: 1

Views: 97

Answers (1)

Vishnu Prasad V
Vishnu Prasad V

Reputation: 1248

In your First DataGrid you are binding the Collection from a method.
private async void FillDataGrid()

But for your second Grid, i guess it is from an event (Considering the arguments for the second event you posted.).
private async void FillContactsDataGrid(object sender, MouseButtonEventArgs e)

This is an event handler right?

There might be a problem because the 'this' in your second method(which is an event handler) points to the object which raised that event and not the window object as like in the first method.

Sorry i didn't make it as a comment. I don't have enough reputation yet to do that.

Hope this helps.

UPDATE Without seeing the entire code i can't find the real problem. The following might be little helpful
@ProgrammingDude is correct. this refers to the class it belongs to.

DataContext = this;

The above line binds your class (which this points to) with the DataContext. This makes the DataGrid binds with the members of the this class
1. Make sure the event handler FillContactsDataGrid is inside the class which this points to
2. Make sure the object ContactsICollectionView is a member of the class which this points to
3. Is ContactsICollectionView a property?

Upvotes: 1

Related Questions