grid.ItemContainerGenerator.ContainerFromIndex(index) returns null for the newly added row in code

2 posts, 0 answers
  1. Nainil
    Nainil avatar
    3 posts
    Member since:
    Feb 2014

    Posted 10 Apr 2014 Link to this post

    I was hoping of obtaining the GridViewRow object of the newly added Item (or GridViewRow) by code. The Telerik GridView's ItemSource is bound to Customers ObservableCollection holding Customer object. On the constructor of the window, I populate the Customers collection with 10 items. This shows up fine on the grid. When I click on the button to add 5  more customers, I can see them been added on the grid, however when I try to access the 11th row using grid.ItemContainerGenerator.ContainerFromIndex(10), it returns null.
    Is there a different way of obtaining the newly added row's index.
  2. Boris
    Admin
    Boris avatar
    276 posts

    Posted 15 Apr 2014 Link to this post

    Hello Nainil,

    I attached a sample project demonstrating how you can get the container of the newly generated GridView item. Please keep in mind that to get а container of an item, it has to be visible in the viewport of the GridView. This is needed because the grid uses virtualization. You can find more information about virtualization in our UI virtualization documentation article.
    The project works as follows: 
    • Run the project
    • Click the Add 1 more row button 
      private Category newCategory;
               
      private void Button_Click(object sender, RoutedEventArgs e)
      {
          this.xRadGridView.BeginInsert();
      }
       
      private void xRadGridView_AddingNewDataItem(object sender, Telerik.Windows.Controls.GridView.GridViewAddingNewEventArgs e)
      {
          this.newCategory = new Category() { Name = "Added Row" };
          e.NewObject = this.newCategory;
      }
    • then click the Get new row container button 
      public void OnClickGetContainer(object sender, RoutedEventArgs e)
      {
          if (this.newCategory != null)
          {
              var container = this.xRadGridView.ItemContainerGenerator.ContainerFromItem(this.newCategory);
                                       
              if (container == null)
              {
                  this.xRadGridView.ScrollIntoView(this.newCategory);
              }
       
              if (container != null)
              {
                  MessageBox.Show("Successfully generated Container");
              }
          }
      }
    • then a success message should come up

    Please let us know if this helps and if you have issues, you can modify the sample project and sent it back to us.


    Regards,
    Boris Penev
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top