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

How set Selected on Row from source

8 Answers 213 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Anton Samarin
Top achievements
Rank 1
Anton Samarin asked on 22 Nov 2008, 08:52 AM
How i can select row in RadGridView in SelectionChanged event (i want to cancel selection)? I set SelectedItem and CurrentItem, but grid not display that this item is selected.

May be i can make necessary inspections before call SelectionChanged (but i can find method, which call before)?

Could you help me?!


8 Answers, 1 is accepted

Sort by
0
Rosi
Telerik team
answered on 24 Nov 2008, 04:53 PM
Hello Anton,

I suggest you find the Record corresponding to the Row and set its IsSelected property to true.

The following code illustrates how you can cancel the selection and select another row:
void radGridViewSelection_SelectionChanged(object sender, SelectionChangeEventArgs e)   
{   
  if (this.radGridViewSelection.SelectedRecords.Count > 0)   
  {   
    this.radGridViewSelection.SelectedRecords[0].IsSelected = false;   
    radGridViewSelection.Records[3].IsSelected=true; //this will always select the 4th row.  
 
  }   
}  

Regards,
Rosi
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Anton Samarin
Top achievements
Rank 1
answered on 29 Nov 2008, 12:48 PM
I know about this way, but it work not fine, because if you (in your example) select row number 6 ( for example) and after that click to 4th row you again call void radGridViewSelection_SelectionChanged - it is not correct work, i think.
If you select row number 6 ( for example) and click on it again you will can to edit it, but selection will be in 4th row!

I thought you know about this feature
0
Rosi
Telerik team
answered on 01 Dec 2008, 02:42 PM
Hi Anton Samarin,

I am sorry for the misunderstanding.

The problem that you have described is actually a bug in the RadGridView control which will be fixed for the Service Pack. The issue is that the SelectionChanged event handler is executed too early. This is the reason why the code executed in the event handler will not have an effect.

You can workaround the problem by executing the code selecting the item asynchronously.

The following code illustrates how to achieve this:
     private void radGridViewSelection_SelectionChanged(object sender, SelectionChangeEventArgs e)  
        {  
            this.Dispatcher.BeginInvoke(new SelectRow(this.changeSelectedRow), System.Windows.Threading.DispatcherPriority.Background, null);  
        }  
 
        delegate void SelectRow();  //this is the asynchronous delegate
 
 
        private void changeSelectedRow()  
        {   
             //your logic here
            if (this.radGridViewSelection.SelectedRecords.Count > 0 && this.radGridViewSelection.SelectedRecords[0] != radGridViewSelection.Records[3])  
            {  
                this.radGridViewSelection.SelectedRecords[0].IsSelected = false;   
                radGridViewSelection.SelectedItem = radGridViewSelection.Items[3];  
 
            }  
        } 

You can also find the attached project for a reference.

Your Telerik points have been updated for your involvement.

Regards,
Rosi
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Anton Samarin
Top achievements
Rank 1
answered on 02 Dec 2008, 05:59 AM
ok, thank you very much. i try to make some correction for my case.

0
Anton Samarin
Top achievements
Rank 1
answered on 18 Dec 2008, 10:05 AM
Hello, i can't use your sample, because after click i 'come' in EditMode.

And I have a question. Why If i set radGridViewSelection.ItemsSource in window constructor

//example from your project
public Window1()
{
InitializeComponent();
this.radGridViewSelection.ItemsSource = GetObjectData();
}

radGridViewSelection.Items set items. But if i set radGridViewSelection.ItemsSource after, for example, button click radGridViewSelection.Items.count = 0 (but radGridViewSelection.ItemsSource.count != 0)?

I hope you understand me.
0
Accepted
Rosi
Telerik team
answered on 18 Dec 2008, 12:19 PM
Hi Anton Samarin,

The Q3 Service Pack release will be released by the end of the day. I suggest you download it from your account and try it. In this case you will not need the workaround from the previous message.You can then change the SelectedItem in the SelectionChanged event handler of RadGridView without using asynchronous method.

As for the second problem I suggest you use radGridViewSelection.Records.Count instead of radGridViewSelection.Items.Count. In this way you can safety get the count of the records.

Hope this helps.

Regards,
Rosi
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Anton Samarin
Top achievements
Rank 1
answered on 18 Dec 2008, 12:41 PM
ok, thank you very much.
My second problem connected with SelectionChanged, because Q3 Service Pack solve two problems.
0
Rosi
Telerik team
answered on 18 Dec 2008, 04:57 PM
Hi Anton,

The Service Pack is available for download now. You can try it and let us know if you experience any problems.

Regards,
Rosi
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
GridView
Asked by
Anton Samarin
Top achievements
Rank 1
Answers by
Rosi
Telerik team
Anton Samarin
Top achievements
Rank 1
Share this question
or