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

Can't select SelectedItem in code

1 Answer 95 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jon
Top achievements
Rank 1
Jon asked on 26 Feb 2010, 02:33 PM
Hi..
When I set the SelectedItem... it does not change... any ideas... thanks

I'm trying to find a specific item in grid and set that item to the SelectedItem..      Is the following code the correct method...?
The object is found but the selecteditem does not get set.

   List<inv_mstr> lstFind = this.grid.ItemsSource as List<inv_mstr>;

 

                List<inv_mstr> lstFind2 = (lstFind.Where(i => i.inv_no == Convert.ToInt32(sch.ApptID)).Take(1)).ToList();

 

                this.grid.OnApplyTemplate();

 

                this.grid.SelectedItem = lstFind2.Take(1);

1 Answer, 1 is accepted

Sort by
0
Milan
Telerik team
answered on 01 Mar 2010, 12:17 PM
Hi Jon The Nerd,

If you try to set SelectedItem too early (before RadGridView is shown for the first time) the SelectedItem will not be changed. We will fix this problem as soon as possible. Currently you could workaround the problem by using the DataLoaded event:

private bool initialized = false;
  
private void clubsGrid_DataLoaded(object sender, System.EventArgs e)
{
    // DataLoaded will be called multiple times 
    //so we make sure that our logic will be executed only once
    if (initialized)
        return;
  
    initialized = true;
  
    List<inv_mstr> lstFind = this.grid.ItemsSource as List<inv_mstr>;
    List<inv_mstr> lstFind2 = (lstFind.Where(i => i.inv_no == Convert.ToInt32(sch.ApptID)).Take(1)).ToList();
  
    this.grid.SelectedItem = lstFind2.Take(1);
}

Also, it is not recommended to call OnApplyTemplate manually.

Sincerely yours,
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
Jon
Top achievements
Rank 1
Answers by
Milan
Telerik team
Share this question
or