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

[Solved] GridView_SelectionChanging event?

1 Answer 99 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.
Mike McDougall
Top achievements
Rank 1
Mike McDougall asked on 30 Sep 2009, 11:56 AM
I would like a way to validate the current row when selecting a different row and potentially cancel that navigation.  Is this possible?

1 Answer, 1 is accepted

Sort by
0
Milan
Telerik team
answered on 01 Oct 2009, 03:51 PM
Hello Mike McDougall,

Yes, it is possible to implement such behavior. Here is a sample implementation:

private bool changingSelection = false;  
 
void gridView_SelectionChanged(object sender, SelectionChangeEventArgs e)  
{  
    if (changingSelection)  
        return;  
 
    if (e.AddedItems.Count <= 0)  
        return;  
 
    changingSelection = true;  
 
    // custom validation logic  
    // do not allow the first item to be selected
    if (this.gridView.Items.IndexOf(e.AddedItems[0]) == 0)  
    {  
        this.gridView.SelectedItems.Remove(e.AddedItems[0]);  
 
        // revert old selection if desired  
        foreach (var previouslySelectedItem in e.RemovedItems)  
            this.gridView.SelectedItems.Add(previouslySelectedItem);  
    }  
 
    changingSelection = false;  

You just have to subscribe to the SelectionChanged event and change the SelectedItems collection according to your rules.

Hope this works.

All the best,
Milan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
GridView
Asked by
Mike McDougall
Top achievements
Rank 1
Answers by
Milan
Telerik team
Share this question
or