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

Problem with Delete CommandBinding

11 Answers 592 Views
GridView
This is a migrated thread and some comments may be shown as answers.
PW
Top achievements
Rank 1
PW asked on 27 May 2010, 12:33 AM
We're having problems with a Delete button that we've added as a column in a GridView.  We're using a CommandBinding, but the DeleteExecuted function where we remove the item from the collection doesn't get called, yet the row automagically disappears from the GridView.  I can be debugging for a while trying to delete a bunch of rows (one-at-a-time) without ever hitting a breakpoint set at the very top of the DeleteExecuted function.  During this time the rows are consistently being removed from the GridView - but the DeleteExecuted function isn't called.  It's as if something within the GridView is automagically removing the row and then setting the event as handled and not routing on to the bound command.  Then out of the blue during the same debugging session it will *sometimes* hit my breakpoint, and work reliably from that point on.  Incredibly frustrating. 

For the record, I'm using the new RadControls for WPF 2010 Q1 release build, running VS 2008 and .NET 3.5.  Updating to VS 2010 and .NET 4 is not an option for us at this time.  I'm about 90% sure that this was working reliably before we upgraded to the 2010 Q1 release, but unfortunately rolling back isn't an attractive option because we needed a fix that became available in the 2010 Q1 release.

I'm hoping someone out there can throw me some bread crumbs on what might be going on and how to fix it.

Here's where we define the button for the delete column:
<telerik:GridViewColumn Header="Delete" IsVisible="True">  
    <telerik:GridViewColumn.CellTemplate> 
        <DataTemplate> 
            <Button Width="18" Height="18" Margin="5,0"   
                    Command="ApplicationCommands.Delete" CommandParameter="{Binding}" > 
                <Button.Content> 
                    <Grid Width="8.083" Height="8.664" VerticalAlignment="Center" HorizontalAlignment="Center">  
                        <Path Fill="{x:Null}" Stretch="Fill" Stroke="#FF000000" Margin="0.083,0.664,0,0" Data="M0.50001547,0.5 L6.5000797,6.5000169 M6.5000155,0.5 L0.5,6.5000704" StrokeThickness="2"/>  
                        <Path Fill="#FFCE3527" Stretch="Fill" Stroke="#FFCD3527" Margin="0,0,0.083,0.664" Data="M0.50001547,0.5 L6.5000797,6.5000169 M6.5000155,0.5 L0.5,6.5000704" StrokeThickness="2"/>  
                    </Grid> 
                </Button.Content> 
            </Button> 
        </DataTemplate> 
    </telerik:GridViewColumn.CellTemplate> 
</telerik:GridViewColumn> 
 

And the pertinent stuff from the code-behind:

public ucChartRegionGrid2()  
{  
    InitializeComponent();  
 
    this.Loaded += new RoutedEventHandler( ucChartRegionGrid2_Loaded );  
    this.CommandBindings.Add( new CommandBinding( ApplicationCommands.Delete,   
                                this.DeleteExecuted, this.DeleteCanExecute ) );  
    radGridViewDataBinding.SelectionChanged += new   
        EventHandler<Telerik.Windows.Controls.SelectionChangeEventArgs>(radGridViewDataBinding_SelectionChanged);  
}  
 
public void DeleteCanExecute(object sender, CanExecuteRoutedEventArgs e)  
{  
    e.CanExecute = true;  
}  
 
public void DeleteExecuted(object sender, ExecutedRoutedEventArgs e)  
{  
    // set a breakpoint here.  It’s not hit, but the row disappears from the grid!?  
    RegionEvent2 deleteRange = e.Parameter as RegionEvent2;  
 
 


Thanks in advance!

11 Answers, 1 is accepted

Sort by
0
Milan
Telerik team
answered on 27 May 2010, 07:01 AM
Hello Darcy Vaughan,

Your code is not called because RadGridView handles the ApplicationCommands.Delete command internally - it deletes all selected items. We have exposed two events called Deleting and Deleted which can help you react to a Delete request. 

Hope this helps.


Best wishes,
Milan
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
Sivasankar Tanakala
Top achievements
Rank 1
answered on 24 Aug 2010, 06:16 PM
Actually I have similar problem, but not exactly the same. I have a Delete Button (Like a TRASH Icon) as the first column. But when I click on the button the Row of the Button CLick is not getting deleted, whereas the row which is currently selected is getting deleted. How to delete the row on which the Command Button Click happened?
0
Milan
Telerik team
answered on 25 Aug 2010, 08:35 AM
Hello Sivasankar Tanakala,

You should specify CommandParameter to be able to delete the respective item. For example:

<telerik:GridViewColumn>
                     <telerik:GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <telerik:RadButton Content="Delete" Command="telerikGrid:RadGridViewCommands.Delete" CommandParameter="{Binding}" />
                        </DataTemplate>
                    </telerik:GridViewColumn.CellTemplate>
                </telerik:GridViewColumn>


Regards,
Milan
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
Siva Prakash
Top achievements
Rank 1
answered on 09 Dec 2011, 11:47 AM
Hi,

In my project, I want to invoke the RadGridViewCommands.Delete command in a RadMenuItem Click event.
As of now, I am doing like this-->

RadGridViewCommands.Delete.Execute(radgridview1.SelectedItems);

But this thing not triggering my radgridview's Deleting event in one scenario.

My scenario is like this :
  • Directly right click on the grid without selecting any row or cell. Then a context menu will open.
  • Click delete menu item in that. Now I am getting the CanExecute value of the Delete command as false here. Thats why deleting event of the radgridview not getting triggered.


But when you first select a cell and then right click, then the CanExecute is true and deleting event  is getting triggered. So can you please tell me in which scenarios the CanExecute method of the Delete command will return false.


Regards
K Siva Prakash.
0
Maya
Telerik team
answered on 12 Dec 2011, 07:45 AM
Hello Siva Prakash,

Actually, that would be the expected behavior since when you open the context menu, no item is selected and the grid is not aware which item to remove. What you can try is to handle Opening event of RadContextMenu, find the underlying row and set its IsSelected property. It could be something similar to:

private void RadContextMenu_Opening(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            RadContextMenu menu = sender as RadContextMenu;
            GridViewRow row = menu.GetClickedElement<GridViewRow>();
            if(row != null)
            {
                row.IsSelected = true;
            }
        }

Will that approach suit your needs ? 
 

Kind regards,
Maya
the Telerik team

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

0
Siva Prakash
Top achievements
Rank 1
answered on 13 Dec 2011, 07:27 PM
Hi,

I have already kept the statement of "row.IsSelected=true" in the radcontextmenu openeing event. The row was also getting selected on right clicking on it. But the actual issue I am facing was, the CanExecute() method of the Delete command was returning false, that's why it's not invoking the deleting and delete events of the grid. 
So can you please suggest me the cases in which this CanExectue() method will return false. 

Thanks & Regards
K Siva Prakash.
0
Maya
Telerik team
answered on 14 Dec 2011, 08:19 AM
Hello Siva Prakash,

I have tried to reproduce the issue you reported, but I was not able to. Could you take a look at the sample attached and let me know in case I am missing something ? Can you reproduce the same behavior on this application ?
 

All the best,
Maya
the Telerik team

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

0
bhargava
Top achievements
Rank 1
answered on 17 Feb 2015, 08:53 PM
Hi Maya,
I am having the same problem. This is my code sample
    
<telerik:RadContextMenu.ContextMenu>
            <telerik:RadContextMenu Opening="RadContextMenu_Opening">
                <telerik:RadMenuItem Header="Add Product"
                                     Command="telerikGrid:RadGridViewCommands.BeginInsert"
                                     CommandTarget="{Binding ElementName=purchaseItemGridView}"
                                     IsEnabled="{Binding CanAddItem, Source={StaticResource purchaseItemsViewModel}}"/>
                <!--Delete Product is made invisible until the issue related to Enable/Disable is solved. #615789-->
                <telerik:RadMenuItem Header="Delete Product"
                                     Command="telerikGrid:RadGridViewCommands.Delete"
                                     CommandTarget="{Binding ElementName=purchaseItemGridView}"
                                     IsEnabled="{Binding CanDeleteItem, Source={StaticResource purchaseItemsViewModel}}"/>
            </telerik:RadContextMenu>
        </telerik:RadContextMenu.ContextMenu>

I am changing the visibility of delete in my pitemsVM 
public bool CanDeleteItem
      {
          get { return _canDeleteItem; }
          set
          {
              if (_canDeleteItem != value)
              _canDeleteItem = value;
             this.OnPropertyChanged(() => CanDeleteItem);
          }
      }

When I am debugging, it works as expected .but, without attaching a debugger I am not able to see the delete on right click.
Thanks
Bhargav
0
Maya
Telerik team
answered on 18 Feb 2015, 01:21 PM
Hello Bhargav,

Could you clarify what scenario you want to achieve ? When do you want to disable/ enable the command ? 


Regards,
Maya
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
bhargava
Top achievements
Rank 1
answered on 18 Feb 2015, 02:27 PM
Hi Maya,
I am able to see add and delete product button in context menu when I am debugging and context menu works as expected. When I don't attach any debugger, I only see a add product button enabled, not delete product button even though there are items in the grid.

I want to see the delete product button enabled without attaching any debugger.

I have used your above project to implemented  the selected item in opening too.
when no debugger is attached:

when debugger is attached:

I am debugging using VS 2013.

Thanks,
Bhargav
0
Maya
Telerik team
answered on 18 Feb 2015, 03:53 PM
Hello,

That sounds a bit strange. I updated the project previously attached with the code-snippets you sent. Does this correspond to your scenario  ? When do you want to disable/enable the command ? Could you modify the project so that it illustrates the behavior ? 



Regards,
Maya
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
GridView
Asked by
PW
Top achievements
Rank 1
Answers by
Milan
Telerik team
Sivasankar Tanakala
Top achievements
Rank 1
Siva Prakash
Top achievements
Rank 1
Maya
Telerik team
bhargava
Top achievements
Rank 1
Share this question
or