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

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

1 Answer 456 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Nainil
Top achievements
Rank 1
Nainil asked on 10 Apr 2014, 09:10 PM
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.

1 Answer, 1 is accepted

Sort by
0
Boris
Telerik team
answered on 15 Apr 2014, 10:29 AM
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.

 
Tags
GridView
Asked by
Nainil
Top achievements
Rank 1
Answers by
Boris
Telerik team
Share this question
or