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

Cancel Row Changing for RadGridView

5 Answers 600 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Yavor
Top achievements
Rank 1
Yavor asked on 28 Apr 2010, 03:26 PM

Hi,
I am using RadGridView in a desktop WPF application. The
ItemsSource property of the grid is set to an instance of QueryableCollectionView. What I need is to hook to an event that is raised whenever the user tries to select a different row in the grid and then be able to cancel the change, depending on some user input. I tried using the CurrentChanging event of the QueryableCollectionView and set “e.Cancel = true”. As this approach works just fine with a normal WPF ListView, RadGridView doesn’t pick up the result from the CurrentChanging event of the QueryableCollectionView and the selection of the RadGridView is still moved to the new row (although the CollectionView’s current item does not change!). Can you give me a solution? Thank you!

5 Answers, 1 is accepted

Sort by
0
Milan
Telerik team
answered on 29 Apr 2010, 09:15 AM
Hello Yavor,

We are considering to add SelectionChanging event which will help a lot in such cases. May I ask you for some feedback about this event? I would like to ask you if you would expect to be able to change the selection in the SelectionChanging event or simply cancel the new selection?

You can workaround the current limitation bu using the SelectionChanged event in the following way:

private bool changingSelection;
  
void playersGrid_SelectionChanged(object sender, SelectionChangeEventArgs e)
{
    if (changingSelection)
        return;
  
    changingSelection = true;
  
      
    // modify selection
    // For example, does not allow firs item to be deselected
    if (e.RemovedItems.Contains(this.playersGrid.Items[0]))
    {
        this.playersGrid.SelectedItems.Add(this.playersGrid.Items[0]);
    }
  
    changingSelection = false;
}


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
Yavor
Top achievements
Rank 1
answered on 29 Apr 2010, 01:38 PM

Thank you Milan! Adding ‘SelectionChangingevent would be really helpful indeed! For this particular purpose, all I need is the Cancel functionality. Generally speaking, I don’t think that anyone would need to select some other row on SelectionChanging event. Moreover, this would seem a rather strange behavior for the UI and would confuse the end user.

I am working on a MVVM desktop application and I am using the RadGridView to display a list of companies. The SelectedItem property of the grid is indirectly bound (through the underlying ViewModel) to a TabControl panel in a master-detail fashion, each tab containing a section with editable properties for the current company. Upon selection change in the ‘master’ control (the RadGridView) I need to check whether the currently edited company has changed and prompt the user to choose among: Save changes | Discard changes | Cancel. If the selection change is allowed, the company is saved to the database and the details for the newly selected company are picked up and displayed in the tab control panel. The tricky part is that the proposed SelectionChanged event gets fired after the SelectedItem is updated on the ViewModel (triggering all its assosiated logic) and it’s all too late to revert at this point. Along with that, the visual selection of the grid is already positioned on the new row and when the confirmation dialog is displayed, the RadGridView beneath it will misleadingly have switched to the new selection. Thanks for the suggestion anyway. I hope this will have a ‘native’ solution in some future version.

Greetings,

Yavor

0
Milan
Telerik team
answered on 03 May 2010, 02:08 PM
Hello Yavor,

Thank you for your valuable feedback. I have updated your Telerik points. 

We will try to introduce SelectionChanging event as soon as possible. I have created e PITS item which can be used to track the status of this feature request. 

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
Roger
Top achievements
Rank 1
answered on 14 Jun 2011, 09:05 PM
I'm not convinced that this is the right answer. The point of using an ICollectionView object is so that the control will know how to handle its events. The RadGridView seems to respond to the ICollectionView CurrentChanged event by selecting the correct item, so why doesn't it respond to the ICollectionView CurrentChanging event and automatically handle the Cancel state?
0
Schokoolero50
Top achievements
Rank 1
answered on 12 Jul 2011, 09:00 AM
I agree with Roger. It is OK as a workaround, but not a real solution to the problem.
We are experiencing the same problem with the RadTreeListView.
With MVVM you shouldn't use all that code behind needed to handle such events (although that can be avoided using RoutedEvents).
If the TwoWay-Binding of the SelectedItem worked correctly, preventing the object the SelectedItem is bound to from changing should result in the grid adjusting (or rather not adjusting) the selected row automatically.

I have tried the given solution as a workaround and it isn't quite perfect.
I couldn't figure out a way to also prevent the rectangle idicating the clicked cell from jumping to the clicked row anyway.
Actually as the SelectionUnit is set to FullRow I don't see why that rectangle appears at all, in my eyes it shouldn't.
Is there any way to shut it of without preventing interaction with the grid completely?
Tags
GridView
Asked by
Yavor
Top achievements
Rank 1
Answers by
Milan
Telerik team
Yavor
Top achievements
Rank 1
Roger
Top achievements
Rank 1
Schokoolero50
Top achievements
Rank 1
Share this question
or