This is a migrated thread and some comments may be shown as answers.

[Solved] deleting a detail does not update master

3 Answers 87 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Jim
Top achievements
Rank 1
Jim asked on 16 Aug 2011, 09:04 AM
I have two grids in a master details scenario
The master class has an observable collection of the details class.
It generally works and if I cursor up and down the master grid the detail grid changes to reflect the correct details.
I implemented a StyleSelector such that the master rows that have detail entries show up in a different colour, this works OK.
However if I delete from the detail such that the master doesn't have any detail entries the grid doesn't pick it up.
Neither if I add a detail to a master that has no detail does that work.
If I reload the application things are then OK.
I assume it's something to do with INotifyCollectionChanged
Do I need to implement this somehow, if so I don;t know how to ?
Help !
Jim

3 Answers, 1 is accepted

Sort by
0
Jim
Top achievements
Rank 1
answered on 17 Aug 2011, 11:10 AM

I have reduced the problem to a small(ish) example.
You can cursor up/down the companies grid and the employees reflects the current company.
Also the style selector shows red if the company has no employees.
However if you select a company with employees and then delete all the employees for that company then the row doesn't change to red.
Again I suspect INotifyCollectionChanged might be the solution but not sure how !
Thanks
Jim




<UserControl x:Class="CollectionChangeNotification.MainPage"
    mc:Ignorable="d"
    xmlns:alloc="clr-namespace:CollectionChangeNotification"
    d:DesignHeight="480" d:DesignWidth="480" xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
  
    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.Resources>
  
            <alloc:RowStyle x:Key="rowStyle">
                <alloc:RowStyle.WithEmployeesStyle>
                    <Style TargetType="telerik:GridViewRow">
                        <Setter Property="Background" Value="LightCyan"/>
                    </Style>
                </alloc:RowStyle.WithEmployeesStyle>
                <alloc:RowStyle.WithoutEmployeesStyle>
                    <Style TargetType="telerik:GridViewRow">
                        <Setter Property="Background" Value="LightCoral" />
                    </Style>
                </alloc:RowStyle.WithoutEmployeesStyle>
            </alloc:RowStyle>
  
        </Grid.Resources>
  
        <Grid.RowDefinitions>
            <RowDefinition Height="244*" />
            <RowDefinition Height="236*" />
        </Grid.RowDefinitions>
  
        <telerik:RadGridView ItemsSource="{Binding}" RowStyleSelector="{StaticResource rowStyle}" AutoGenerateColumns="False" HorizontalAlignment="Left" Name="RadGridView1" VerticalAlignment="Bottom" Height="244" Width="480">
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn DataMemberBinding="{Binding CompanyName, Mode=TwoWay}" Header="Company Name"/>
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>
        <telerik:RadGridView ItemsSource="{Binding ElementName=RadGridView1, Path=SelectedItem.Employees}" ShowInsertRow="True" CanUserInsertRows="True"  Grid.Row="1" HorizontalAlignment="Left" Name="RadGridView2" VerticalAlignment="Top" Height="236" Width="480" />
    </Grid>
</UserControl>
Imports System.ComponentModel
Imports System.Collections.ObjectModel
Imports Telerik.Windows.Controls
  
Partial Public Class MainPage
    Inherits UserControl
  
    Public Sub New()
        InitializeComponent()
  
        'Set up some data
        Dim electricCompany As Company = New Company("The Electric Company")
        Dim fred As New Employee("Fred Bloggs", 45)
        Dim anne As New Employee("Anne Smith", 20)
        electricCompany.Employees.Add(fred)
        electricCompany.Employees.Add(anne)
  
        Dim noEmployeeCompany As Company = New Company("The No Employee Company")
  
        Dim gasCompany As Company = New Company("The Gas Company")
        Dim peter As New Employee("Peter Perfect", 30)
        Dim george As New Employee("Georgie Porgie", 19)
        gasCompany.Employees.Add(peter)
        gasCompany.Employees.Add(george)
  
        Dim myCompanies As ObservableCollection(Of Company) = New ObservableCollection(Of Company)
        myCompanies.Add(electricCompany)
        myCompanies.Add(noEmployeeCompany)
        myCompanies.Add(gasCompany)
  
        DataContext = myCompanies
  
    End Sub
  
End Class
'**********************************************************************************************
Public Class Company
  
    Public Event PropertyChanged As PropertyChangedEventHandler
  
    Private Sub OnPropertyChanged([property] As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs([property]))
    End Sub
  
    Private m_CompanyName As String
    Private m_Employees As ObservableCollection(Of Employee)
  
    Public Property CompanyName As String
        Get
            Return m_CompanyName
        End Get
        Set(ByVal value As String)
            If m_CompanyName <> value Then
                m_CompanyName = value
                Me.OnPropertyChanged("CompanyName")
            End If
        End Set
    End Property
  
    Public Property Employees As ObservableCollection(Of Employee)
        Get
            Return m_Employees
        End Get
        Set(ByVal value As ObservableCollection(Of Employee))
            m_Employees = value
            Me.OnPropertyChanged("Employees")
        End Set
    End Property
  
    Public Sub New()
        MyBase.New()
        m_Employees = New ObservableCollection(Of Employee)
    End Sub
  
    Public Sub New(companyName As String)
        MyBase.New()
        m_CompanyName = companyName
        m_Employees = New ObservableCollection(Of Employee)
    End Sub
  
End Class
'**********************************************************************************************
Public Class Employee
    Public Event PropertyChanged As PropertyChangedEventHandler
  
    Private Sub OnPropertyChanged([property] As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs([property]))
    End Sub
  
    Private m_Name As String
    Private m_Age As Integer
  
    Public Property Name As String
        Get
            Return m_Name
        End Get
        Set(ByVal value As String)
            If m_Name <> value Then
                m_Name = value
                Me.OnPropertyChanged("Name")
            End If
        End Set
    End Property
  
    Public Property Age As Integer
        Get
            Return m_Age
        End Get
        Set(ByVal value As Integer)
            If m_Age <> value Then
                m_Age = value
                Me.OnPropertyChanged("Age")
            End If
        End Set
    End Property
  
    Public Sub New()
        MyBase.New()
    End Sub
  
    Public Sub New(name As String, age As Integer)
        MyBase.New()
        m_Name = name
        m_Age = age
    End Sub
  
End Class
  
'**********************************************************************************************
Public Class RowStyle
    Inherits StyleSelector
    Public Overrides Function SelectStyle(ByVal item As Object, ByVal container As DependencyObject) As Style
        Dim ans As Style = Nothing
        If TypeOf item Is Company Then
            Dim company As Company = TryCast(item, Company)
            If company.Employees.Count > 0 Then
                Return WithEmployeesStyle
            Else
                Return WithoutEmployeesStyle
            End If
        End If
        Return ans
    End Function
    Public Property WithEmployeesStyle() As Style
        Get
            Return m_WithEmployeesStyle
        End Get
        Set(ByVal value As Style)
            m_WithEmployeesStyle = value
        End Set
    End Property
    Private m_WithEmployeesStyle As Style
    Public Property WithoutEmployeesStyle() As Style
        Get
            Return m_WithoutEmployeesStyle
        End Get
        Set(ByVal value As Style)
            m_WithoutEmployeesStyle = value
        End Set
    End Property
    Private m_WithoutEmployeesStyle As Style
End Class
0
Maya
Telerik team
answered on 22 Aug 2011, 09:38 AM
Hello Jim,

The row style selector will be invoked on change of the property of the item, not on change of the collection. What you may try is to expose a new property holding the count of the child items and set it on CollectionChanged event of the child collection. 
I am sending you a sample project illustrating the suggested approach.

Kind regards,
Maya
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

0
Jim
Top achievements
Rank 1
answered on 23 Aug 2011, 12:44 PM
Thanks for the reply, I will look at this when I get a spare moment.
Regards
Jim
Tags
GridView
Asked by
Jim
Top achievements
Rank 1
Answers by
Jim
Top achievements
Rank 1
Maya
Telerik team
Share this question
or