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

[Solved] Change Selected Index from code

5 Answers 407 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.
JungTae
Top achievements
Rank 1
JungTae asked on 30 Sep 2010, 02:25 PM
When I was using the silverlight DataGrid, I had a following code

this.PricingModelGrid.SelectedIndex = 0;

how however, I'm trying to replace all my datagrid with RadGridView, and I need to know how to change the selected index of a RadGridView from the c# code.

5 Answers, 1 is accepted

Sort by
0
Accepted
Pavel Pavlov
Telerik team
answered on 30 Sep 2010, 04:37 PM
Hi JungTae,

Something like :

private void SetSelectedItemByIndex(int index)
    {
        this.radGridView1.SelectedItem = this.radGridView1.Items[index];
    }

should do the trick.

All the best,
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
HDD
Top achievements
Rank 1
answered on 07 Oct 2010, 07:38 PM
Pavel,
This is nice to know. Also I was trying the reverse of this. That is, getting the selected index of the RadGridView.
This is what I have right now:
int selectedindex = radGridView.SelectedItems.IndexOf(radGridView.SelectedItem);

But for some reason I always get 0 (zero) as the value for the selectedindex, which is the first row of the grid.
What could be the cause.
Thanks,
HDD.
0
JungTae
Top achievements
Rank 1
answered on 07 Oct 2010, 07:44 PM
If you are using LINQ, you can use this code. I am currently using it in many places in my code


int index = this.myGrid.Items.IndexOf(this.myGrid.SelectedItem)
0
Prakash
Top achievements
Rank 1
answered on 22 Oct 2010, 12:31 PM
I have set the Itemsource to silverlight gird... in the same function i going to select the row by passing the row index.

like this.radGridView1.SelectedItem = this.radGridView1.Items[1];

It says Index out of range, but i have 5 records in the grid.

But when i pass the same on button click it works fine..

could u please help me out..
0
Milan
Telerik team
answered on 25 Oct 2010, 09:15 AM
Hi Prakash,

I believe you are trying to access the Items before they are actually available. Could you please try to set the SelectedItem in an event handler for DataLoaded.


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
Tags
GridView
Asked by
JungTae
Top achievements
Rank 1
Answers by
Pavel Pavlov
Telerik team
HDD
Top achievements
Rank 1
JungTae
Top achievements
Rank 1
Prakash
Top achievements
Rank 1
Milan
Telerik team
Share this question
or