Remove Entity from RadDomainDataSource

4 posts, 1 answers
  1. Dejan
    Dejan avatar
    5 posts
    Member since:
    Oct 2011

    Posted 13 Oct 2011 Link to this post

    Hi all

    I want to implement custom deleting of Entity using RadDomainDataSource and RadGridView:
    <telerik:RadDomainDataSource 
        Name="OrganizerDomainDataSource" 
        AutoLoad="False" Height="0"  Width="0"
        DomainContext="{StaticResource OrganizerDomainContext}"
        QueryName="GetLocationSetQuery" PageSize="1000"
        LoadedData="OrganizerDomainDataSource_LoadedData" 
        SubmittedChanges="OrganizerDomainDataSource_SubmittedChanges">
    </telerik:RadDomainDataSource>

    <telerik:RadGridView  
                    Name="LocationDataGrid" 
                    AutoGenerateColumns="False" 
                    Grid.Row="1" Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                    ItemsSource="{Binding Path=DataView, ElementName=OrganizerDomainDataSource}" 
                    RowDetailsVisibilityMode="VisibleWhenSelected" 
                    RowIndicatorVisibility="Collapsed" AlternationCount="2"
                    IsEnabled="{Binding Path=Parent.HasChanges, ElementName=LayoutRoot, Converter={StaticResource NotOperatorValueConverter}}"
                    IsReadOnly="True" ShowGroupPanel="False" IsFilteringAllowed="True"
                    SelectionMode="Single" SelectionUnit="FullRow">
                    <telerik:RadGridView.Columns>
                        <telerik:GridViewDataColumn DataMemberBinding="{Binding Path=Name}" 
                                                                                TextAlignment="Left" HeaderTextAlignment="Left"  
                                                                                Header="{Binding Path=LocationPage_LocationDataGrid_Header_Name, Source={StaticResource PageResources}}"  
                                                                                Width="SizeToCells"
                                                                                IsFilterable="True"
                                                                                ShowDistinctFilters="False"
                                                                                ShowFieldFilters="True"/>
      
                        <telerik:GridViewDataColumn DataMemberBinding="{Binding Path=City}" 
                                                                                Header="{Binding Path=LocationPage_LocationDataGrid_Header_City, Source={StaticResource PageResources}}" 
                                                                                Width="SizeToCells"
                                                                                IsFilterable="True"
                                                                                ShowDistinctFilters="False"
                                                                                ShowFieldFilters="True"/>
      
                        <telerik:GridViewDataColumn DataMemberBinding="{Binding Path=OrganisationPath}" 
                                                                                Header="{Binding Path=LocationPage_LocationDataGrid_Header_CompanyPath, Source={StaticResource PageResources}}"  
                                                                                Width="*"
                                                                                IsFilterable="True"
                                                                                ShowDistinctFilters="False"
                                                                                ShowFieldFilters="True"/>
      
                        <telerik:GridViewDataColumn DataMemberBinding="{Binding Path=RoomSet.Count}" 
                                                                                TextAlignment="Right" HeaderTextAlignment="Right" 
                                                                                Header="{Binding Path=LocationPage_LocationDataGrid_Header_RoomNumber, Source={StaticResource PageResources}}"  
                                                                                Width="SizeToHeader"
                                                                                IsFilterable="True"
                                                                                ShowDistinctFilters="False"
                                                                                ShowFieldFilters="True"/>
      
                        <telerik:GridViewDataColumn 
                            x:Name="functionColumn" 
                            Header="{Binding Path=LocationPage_LocationDataGrid_Header_Functions, Source={StaticResource PageResources}}" Width="SizeToHeader">
                            <telerik:GridViewDataColumn.CellTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{StaticResource ImageButtonStyle}" Click="DeleteItemButton_Click" CommandParameter="{Binding Path=LocationId}">
                                            <Image Source="../../Assets/Images/Icons/btn_delete.png"/>
                                            <ToolTipService.ToolTip>
                                                <ToolTip Content="{Binding Path=LocationPage_LocationDataGrid_Function_Tooltip_Delete, Source={StaticResource PageResources}}"></ToolTip>
                                            </ToolTipService.ToolTip>
                                        </Button>
                                    </StackPanel>
                                </DataTemplate>
                            </telerik:GridViewDataColumn.CellTemplate>
                        </telerik:GridViewDataColumn>
                    </telerik:RadGridView.Columns>
                </telerik:RadGridView>

    In code behind I implemented DeleteButton handler:


    /// <summary>
    /// delete selected location
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void DeleteItemButton_Click(object sender, RoutedEventArgs e)
    {
        var itemId = (Guid)((Button)sender).CommandParameter;
        var location = GetLocationFromDataSource(itemId);
        if (location != null)
        {
            // ClxMessageBox: are you sure?
            var msgBoxDeleting = new ClxMessageBox(ClxMessageBoxType.YesNo)
            {
                Title = GlobalResources.MsgBoxDeleting_Title,
                Message = string.Format(GlobalResources.MsgBoxDeleting_Message, location.Name + "(" + location.OrganisationPath + ")")
            };
            msgBoxDeleting.ShowDialog(result =>
            {
                if (result)
                {
                    LocationDataGrid.Items.Remove(location);
                    OrganizerDomainDataSource.SubmitChanges();
                }
            });
        }
    }

    I did not find a way to remove selected Location directly from OrganizerDomainDataSource so I removed it from LocationDataGrid.

    This implementation actually works.

     

    Only question is: is there a better solution for removing Entity from RadDomainDataSource then this one:

     

    LocationDataGrid.Items.Remove(location);
    OrganizerDomainDataSource.SubmitChanges();

    Regards,
    Dekos

     

  2. Answer
    Rossen Hristov
    Admin
    Rossen Hristov avatar
    2478 posts

    Posted 13 Oct 2011 Link to this post

    Hello Dejan,

    Can you try this:

    this.radDomainDataSource.DataView.Remove(item); // there is RemoveAt(index) as well
    this.radDomainDataSource.SubmitChanges();

    Greetings,

    Ross
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. DevCraft banner
  4. Dejan
    Dejan avatar
    5 posts
    Member since:
    Oct 2011

    Posted 13 Oct 2011 Link to this post

    Thanks Ross!

    Reading other threads at forum (e.g. http://www.telerik.com/community/forums/silverlight/gridview/2-problems-with-grid-raddomaindatasource.aspx ) I realized that messing around with DataView directly can make some thoubles.
     
    Anyway your proposal is working in this scenario.
    Your are not aware of any side efects in using it?

    Regards,
    Dejan
  5. Rossen Hristov
    Admin
    Rossen Hristov avatar
    2478 posts

    Posted 13 Oct 2011 Link to this post

    Hi Dejan,

    It will be fine. When you remove an item from the grid, you will reach the same code eventually, so it's fine.

    Best wishes,
    Ross
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Back to Top