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

[Solved] Deleting cells content

2 Answers 195 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.
Wil
Top achievements
Rank 1
Wil asked on 29 Sep 2010, 11:20 AM
Hi

I think I'm missing something easy, but could not find a solution. I'm working on a grid twoway binded to an ObservableList, and I need to implement some basic functions on the cells: cut, copy, paste, delete, move and so on. While copy and paste are working easy since the last update, I cannot find a way to implement the delete function on cells only. 

What I'd like:
- respond to delete key on keyboard, or context menu command
- delete the content of all the selected cells

What I'm doing:
private void RadContextMenuItemClick(object sender, Telerik.Windows.RadRoutedEventArgs e)
{
    RadContextMenu menu = (RadContextMenu)sender;
    RadMenuItem clickedItem = e.OriginalSource as RadMenuItem;
    GridViewRow row = menu.GetClickedElement<GridViewRow>();
 
    if (clickedItem != null && row != null)
    {
        switch (clickedItem.Name)
        {
            case "rmiDeleteRow":
                rgvIsolateLookup.Items.Remove(row.Item);
                break;
            case "rmiDeleteCell":
                var cells = rgvIsolateLookup.ChildrenOfType<GridViewCell>().Where(c => c.IsSelected);
                foreach (GridViewCell cell in cells)
                {
                    cell.BeginEdit();
                    cell.Value = ""; // i tried with cell.Content = "" too, no success
                    bool t = cell.CommitEdit();
                }
                break;
        }
    }
}

private void rgvIsolateLookup_CellEditEnded(object sender, GridViewCellEditEndedEventArgs e)
{
}


<UserControl x:Class="DocumentManager.Panels.IsolateLookup.pnlIsolateLookup"
    mc:Ignorable="d"
    xmlns:telerikGrid="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView"
    d:DesignHeight="600" d:DesignWidth="800"
    Loaded="UserControlLoaded">
 
    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.RowDefinitions>
            <RowDefinition Height="42"/>
            <RowDefinition />
        </Grid.RowDefinitions>
 
        <StackPanel Grid.Row="0" Orientation="Horizontal" HorizontalAlignment="Center">
            <Button x:Name="cmdNewRow" Content="New Row" Height="25" Click="CmdNewRowClick"></Button>
            <Button x:Name="cmdNewRowCopySelectedRow" Content="New Row from Selected Row" Height="25" Margin="10,0,0,0" Click="CmdNewRowCopySelectedRowClick"></Button>
            <Button x:Name="cmdPasteNew" Content="Paste New" Height="25" Margin="10,0,0,0" Click="CmdPasteNewClick"></Button>
            <Button x:Name="cmdPasteAppend" Content="Paste Append" Height="25" Margin="10,0,0,0" Click="CmdPasteAppendClick"></Button>
        </StackPanel>
         
        <telerik:RadGridView Grid.Row="1" HorizontalAlignment="Stretch" Margin="2,0,0,0" Name="rgvIsolateLookup" VerticalAlignment="Stretch" ItemsSource="{Binding Mode=TwoWay, UpdateSourceTrigger=Default, Path=DataContext, ElementName=rgvIsolateLookup}" Pasting="RgvIsolateLookupPasting" PastingCellClipboardContent="RgvIsolateLookupPastingCellClipboardContent" Copying="RgvIsolateLookupCopying" CopyingCellClipboardContent="RgvIsolateLookupCopyingCellClipboardContent" SelectionUnit="Cell" SelectionMode="Extended" IsSynchronizedWithCurrentItem="True" CellEditEnded="rgvIsolateLookup_CellEditEnded">
            <telerik:RadContextMenu.ContextMenu>
                <telerik:RadContextMenu Opened="RadContextMenuOpened" ItemClick="RadContextMenuItemClick">
                    <telerik:RadMenuItem Header="Cut"/>
                    <telerik:RadMenuItem Header="Copy" Command="telerikGrid:RadGridViewCommands.Copy" CommandTarget="{Binding ElementName=rgvIsolateLookup}"/>
                    <telerik:RadMenuItem Header="Paste" Command="telerikGrid:RadGridViewCommands.Paste" CommandTarget="{Binding ElementName=rgvIsolateLookup}"/>
                    <telerik:RadMenuItem IsSeparator="True" />
                    <telerik:RadMenuItem Header="Delete row" Name ="rmiDeleteRow"/>
                    <telerik:RadMenuItem Header="Delete cell" Name ="rmiDeleteCell"/>
                </telerik:RadContextMenu>
            </telerik:RadContextMenu.ContextMenu>
        </telerik:RadGridView>
    </Grid>
</UserControl>

cell.CommitEdit() launch the CellEditEnded event, so I can check that the cell new value will be "", and that the operation is Commit, but when cell.CommitEdit() returns, the cell value is resetted to the old one (that means, no delete is done) even if "t" is true.

I could try to modify the original item on the observable list, but this doesn't look very "twoway binded" to me.

Any hints on how to solve?
Thanks a lot.

2 Answers, 1 is accepted

Sort by
0
Pavel Pavlov
Telerik team
answered on 29 Sep 2010, 11:49 AM
Hi Wil,

The approach with modifying the original item is the best one for such scenario.

This approach has one drawback - You need to know the type of the original item /its property.

You may implement some type independent logic using reflection, something like :

                Binding b = column.DataMemberBinding;

                property = item.GetType().GetProperty(b.Path.Path);

                //clear the property ....e.g. set to empty string or null

where column is the column owning the cell and item is the original item .

This is as close as we can get. Indeed - changing the value and calling CommitEdit will not work as it will mess up with the DataMemberBinding of the cell.


Sincerely yours,
Pavel Pavlov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Wil
Top achievements
Rank 1
answered on 29 Sep 2010, 12:02 PM
Hi Pavel Pavlov, many thanks for the incredibly quick reply.

If what you propose is the only way, I'll do like that. Anyway, what I don't get is what is the difference between manually editing a cell, and doing it by code.
Tags
GridView
Asked by
Wil
Top achievements
Rank 1
Answers by
Pavel Pavlov
Telerik team
Wil
Top achievements
Rank 1
Share this question
or