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

[Solved] Selected Item on page load

3 Answers 206 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.
Mark Jakes
Top achievements
Rank 1
Mark Jakes asked on 30 Aug 2010, 02:00 AM

Hello Gurus  ;-)

I have a radgridview on my page called PageStockSheet, the itemssource derived from a RIA services class 'product'.

As part of the page I have a button that passes the grids current selected item to a new page, PageCosts, i.e. a 'product' object.

private void Button_Click_1(object sender, RoutedEventArgs e)
{
      navService.Navigate( new PageCosts(((product)griddocs.SelectedItem)));
}

The new page is loaded...

 

public PageCosts( product selectedProd )
{
     selectedproduct = selectedProd;
     InitializeComponent();
}

 

 

Some editing takes place on the new page and the user then clicks on a button to return to PageStockSheet, passing the original 'selectedproduct' as a parameter:

private void buttonReturn_Click(object sender, RoutedEventArgs e)
{
     navService.Navigate(new PageStocksheet(selectedproduct));
}

When PageStockSheet is loaded, I try to select the product in the constructor using the following:


product selectedonstart;
 

public PageStocksheet( product selectedprod) 
{
    InitializeComponent();
    selectedonstart = selectedprod;
    Loaded += new RoutedEventHandler(PageStocksheet_Loaded); 
}
  
void PageStocksheet_Loaded(object sender, RoutedEventArgs e) 
{
   var query = from s in context.GetProductsQuery() 
               orderby s.PDQVSRef descending 
               select s; 
  
    LoadOperation<product> loadopdocs = context.Load(query);
    loadopdocs.Completed += new EventHandler(loadopdocs_Completed); 
}
void loadopdocs_Completed(object sender, EventArgs e) 
{
    LoadOperation<product> loadop = sender as LoadOperation<product>; 
    if (loadop.Error != null) 
    {
        RadWindow.Alert(loadop.Error.Message); 
        return; 
    }
  
    griddocs.ItemsSource = loadop.Entities;
    if (selectedonstart != null) 
    {
        griddocs.SelectedItem = selectedonstart;
        // griddocs.SelectedItem is always NULL, why?
  
        griddocs.ScrollIntoView(griddocs.SelectedItem);
    }    
}

Why is griddocs.SelectedItem always null?

Can someone guide me to the right way to selecting a row of a RadGridView when loading a page correctly?

Thanks

Mark

3 Answers, 1 is accepted

Sort by
0
Milan
Telerik team
answered on 30 Aug 2010, 07:52 AM
Hello Mark Jakes,

This is a know issue which is cause by the fact that the actual data binding of the grid will occur when the grid is first shown. For the time being you can move the code that restored the SelectedItem to an event handler of the DataLoaded event. 


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
Mark Jakes
Top achievements
Rank 1
answered on 30 Aug 2010, 12:53 PM
Hello Milan

Thanks for the reply.  I had already tried subscribing to the DataLoaded event without much success:

void loadopdocs_Completed(object sender, EventArgs e)
{
    LoadOperation<product> loadop = sender as LoadOperation<product>;
  
    if (loadop.Error != null)
    {
        RadWindow.Alert("There was an error");
        RadWindow.Alert(loadop.Error.Message);
        return;
    }
  
    LayoutRoot.DataContext = loadop.Entities;
    griddocs.ItemsSource = loadop.Entities;
  
    griddocs.DataLoaded += (not, used) =>
    {
        if (selectedonstart != null)
        {
            griddocs.SelectedItem = selectedonstart;
            griddocs.ScrollIntoView(griddocs.SelectedItem);
            LayoutRoot.DataContext = griddocs.SelectedItem;
        }
    };
 }

If I debug the above code, the program never hits the dataloaded event so the call to griddocs.SelectedItem is never called.

The first record is always selected.

Any clues?

Mark
0
Milan
Telerik team
answered on 31 Aug 2010, 09:40 AM
Hello Mark Jakes,

DataLoaded event should be called when new ItemsSource is assigned and I am surprised to heat that this is not the case. Cold we have a look at the application?


Greetings,
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
Mark Jakes
Top achievements
Rank 1
Answers by
Milan
Telerik team
Mark Jakes
Top achievements
Rank 1
Share this question
or