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

Programatically expand a specific row's details and select a grid row inside those details

5 Answers 984 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Paul
Top achievements
Rank 1
Paul asked on 23 May 2013, 04:26 PM
I am having a terrible time expanding grid details.  I've looked through the documentation, and message boards (this seems to be a common theme), but nothing has worked so far.  Here's exactly what I want:

gridView.ItemsSource = orgList;
gridView.SelectedItem = orgList[0];
////  gridView.rows[0].Expand(); // <-- Yes, I know this doesn't exist, but pretty please add it.


Forget that last line; my issue here is that setting SelectedItem doesn't do anything.  Eventually I decided that something inside Telerik's code was still processing something and I split it up into a new threaded delegate:

gridView.ItemsSource = orgList;
Action a = delegate {
   gridView.SelectedItem = orgList[0];
};
Thread.Sleep(100);
App.Current.Dispatcher.Invoke(a, null);


This is a terrible solution because it uses a sleep, but it does FINALLY set the row.  Then I began working on expanding the row details:

gridView.RowDetailsVisibilityMode = GridViewRowDetailsVisibilityMode.Visible;
gridView.ItemsSource = orgList;
Action a = delegate {
   gridView.SelectedItem = orgList[0];
   gridView.UpdateLayout();
   var row = gridView.ItemContainerGenerator.ContainerFromItem(gridView.SelectedItem) as GridViewRow;
   if (row != null) {
      row.DetailsVisibility = Visibility.Visible;
      row.IsExpanded = true;
   }
};
Thread.Sleep(100);
App.Current.Dispatcher.Invoke(a, null);


Finally this worked and the grid row gets expanded.  Inside this grid row's details is another RadGridView.  I need to select a specific item here, and scroll it into view.  Here's what I am trying:

var row2 = row.ChildrenOfType<DetailsPresenter>().FirstOrDefault() as DetailsPresenter;
if (row2 != null) {
   var details = row2.Content as RadGridView;
   if (details != null) {
      details.SelectedItem = obj2;
      details.ScrollIntoView(obj2);
   }
}


The issue above is that "details" is always null.  The inner content of the grid has not yet rendered.  

Can't I make Telerik render it's grid synchronously in my method so that setting the item source sets up the grid before execution in my method continues?  Or so that expanding the row sets up the detail template contents before that execution continues?  If not, how can I ensure that it updates the details grid before I attempt to set that row?

5 Answers, 1 is accepted

Sort by
0
Yoan
Telerik team
answered on 28 May 2013, 01:16 PM
Hi Paul,

Please find attached a sample project which meets your requirements.

If you need further assistance on this do not hesitate to contact me.


Regards,
Yoan
Telerik

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Paul
Top achievements
Rank 1
answered on 28 May 2013, 04:46 PM
This does not work at all.  Row 0 is always selected.  The code you sent shows that it should be row 2.
0
Yoan
Telerik team
answered on 30 May 2013, 10:36 AM
Hello Paul,

Can you try to set GridView's property - IsSynchronizedWithCurrentItem to True?

Let me know how this works for you.


Regards,
Yoan
Telerik

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Paul
Top achievements
Rank 1
answered on 30 May 2013, 02:03 PM
Yes, that works in your example only.  It does nothing in mine.  Yours works because you set the ItemsSource in advance.  Per the OP, I need to specify the ItemsSource, then immediately pick the row.  But internally Telerik's grid is threading something out in order to create the rows and objects.  

The real OP here is asking "how do I force Telerik's grid to run in the UI thread only". As Such:

grid.ItemsSource = objList;  //  This doesn't do anything if you debug through.  The grid doesn't render right away.
grid.SelectedItem = objList[34]; // This does not work EVER.  Add it to a button click and do it seconds later, and it works.

After a few more hours of messing around, here is the solution I found.  I do not like it, but it works.

First, set both the parent grid, and the detail templated grid to have IsSynchronizedWithCurrentItem to true as you mention.

Then setup the main grid's ItemSource and force row selection through a delegate.

gridView.ItemsSource = orgList;
 
// If we brought back one item, we should expand it.
if (orgList.Count == 1) {
 
   Action a = delegate {
      gridView.SelectedItem = orgList[0];
      gridView.UpdateLayout();
      var row = gridView.ItemContainerGenerator.ContainerFromItem(gridView.SelectedItem) as GridViewRow;
      if (row != null) {
         row.DetailsVisibility = Visibility.Visible;
         row.IsExpanded = true;
      }
 
      // If we searched for a customer, we should select that customer.
      if (!string.IsNullOrWhiteSpace(cust)) {
         row.Tag = cust;
         row.Loaded += new RoutedEventHandler(row_Loaded);
      }
   };
 
   App.Current.Dispatcher.Invoke(a, null);
}

In the code above, the 'cust' variable is a string that tells me which child detail grid row to select.

void row_Loaded(object sender, RoutedEventArgs e) {
 
   // Remove the event from firing again
   var row = (GridViewRow)sender;
   row.Loaded -= new RoutedEventHandler(row_Loaded);
 
   // Pull the child grid which will finally exist
   var childGrid = row.ChildrenOfType<RadGridView>().First();
 
   // The child grid has not rendered yet due to some threading going on inside Telerik.
   // Yet another hack - delegate ran at the end of the current stack
 
   Action a = delegate {
      // Find, select, and pull into the view
      childGrid.SelectedItem = orgList[0].ShopList.FirstOrDefault(o => (o.custno.Equals(row.Tag.ToString(), StringComparison.CurrentCultureIgnoreCase)));
      childGrid.ScrollIntoView(childGrid.SelectedItem);
   };
 
   App.Current.Dispatcher.Invoke(a, null);
}

This finally selects the child grid row.
0
Yoan
Telerik team
answered on 04 Jun 2013, 09:13 AM
Hi Paul,

I am glad to hear that you have resolved the issue. Thank you for sharing the solution with the community.


Regards,
Yoan
Telerik

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
GridView
Asked by
Paul
Top achievements
Rank 1
Answers by
Yoan
Telerik team
Paul
Top achievements
Rank 1
Share this question
or