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

[Solved] RadGridView - ItemContainerGenerator.ContainerFromItem is null

13 Answers 809 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.
Sumati
Top achievements
Rank 1
Sumati asked on 22 Jun 2010, 10:15 AM
I have agrid and two menu items in my application: "Collapse All/Expand" to show rowdetails
I have following methods in my application:

private void Menu_Click(object sender, RadRoutedEventArgs e)
        {
            RadMenuItem m1 = (RadMenuItem)sender;
            if (m1.Header.ToString() == "Collapse All")
            {
                foreach (CrossReferralItem item in referralGrid.Items)
                {
                    var row = (GridViewRow)this.referralGrid.ItemContainerGenerator.ContainerFromItem(item);
                    if (row != null)
                    {
                        row.DetailsVisibility = Visibility.Collapsed;
                    }
                }
                referralGrid.SelectedItem = null;
            }
            else
            {
                foreach (CrossReferralItem item in referralGrid.Items)
                {
                    var row = (GridViewRow)this.referralGrid.ItemContainerGenerator.ContainerFromItem(item);
                    if (row != null)
                    {
                        row.DetailsVisibility = Visibility.Visible;
                    }
                }
            }
        }

        private void OnClick(object sender, MouseButtonEventArgs e)
        {
            var selectedRow = (GridViewRow)this.referralGrid.ItemContainerGenerator.ContainerFromItem(this.referralGrid.SelectedItem);
            if (selectedRow != null)
            {
                if (selectedRow.DetailsVisibility == Visibility.Visible)
                {
                    selectedRow.DetailsVisibility = Visibility.Collapsed;
                }
                else
                {
                    selectedRow.DetailsVisibility = Visibility.Visible;
                }
            }
        }

 

 



The issue is that if user clicks on one row ,then row details is visible which is fine but after that 'Collapse All' menu item will not work. It will not collapse the row visibility of the previuos selected row. This works sometimes and sometimes not. It happens because

 

 

 

 

ItemContainerGenerator.ContainerFromItem

 

is returned as null for few rows. How to solve this issue?

 

 

13 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 22 Jun 2010, 11:36 AM
Hi,

 Generally this will return null if the item is outside of the view area. 

Regards,
Vlad
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
Sumati
Top achievements
Rank 1
answered on 22 Jun 2010, 03:51 PM
We do not restrict number of rows on a grid...so how can I get instance for all rows in RadGridView....?
0
Sumati
Top achievements
Rank 1
answered on 23 Jun 2010, 06:00 AM
I was able to solve this issue by specifying:

EnableRowVirtualization

 

="False" EnableColumnVirtualization="False"

 


for my grid in *.xaml file
0
Pawel
Top achievements
Rank 1
answered on 15 Jul 2010, 08:39 PM
Hi,
I have similar problem - I want to expand/collapse rows selected via GridViewSelectColumn, in response to a button click.
Turning off virtualization is not an option because of performance reasons.
Probably there is some way to do this even if ItemContainerGenerator.ContainerFromItem returns null because the collapsed state is always correctly restored when scrolling so I assume that the collapsed state is not lost even if ItemContainerGenerator.ContainerFromItem is null because then the expanded rows would be collapsed when they leave current view. The question is if this property is exposed and can be modified ?

regards
Pawel
0
Milan
Telerik team
answered on 16 Jul 2010, 05:51 AM
Hello Pawel,

Just a quick question: Are you dealing with hierarchical rows or with row details?


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
0
Pawel
Top achievements
Rank 1
answered on 16 Jul 2010, 06:35 AM
Hi,
I'm dealing with row details

regards
Pawel
0
Milan
Telerik team
answered on 20 Jul 2010, 07:05 AM
Hi Pawel,

Row details have a special mode which allows them to expand automatically when their parent row is selected. Setting RowDetailsVisibilitymode to VisisbleWhenSelected which enable this functionality and you will be able to expand details with the help of our GridViewSelectColumn.

Best wishes,
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
Pawel
Top achievements
Rank 1
answered on 20 Jul 2010, 07:41 AM
This will result in expansion of row details at exact time when the row is selected. I need to have the rows expanded only when the "Expand" button which is outside the grid is clicked. This is because "Expand" is not the only collective operation that I will be implementing and selected row does not necessarily need to be expanded.

regards
Pawel
0
Milan
Telerik team
answered on 20 Jul 2010, 08:04 AM
Hi Pawel,

This functionality is not supported by the grid out of the box but I have prepared a sample solution which demonstrated a possible implementation. 


Kind 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
Pawel
Top achievements
Rank 1
answered on 20 Jul 2010, 02:42 PM
Hi,
Thank you

regards
Pawel
0
Frédéric
Top achievements
Rank 1
answered on 26 Jan 2011, 12:33 PM
Hi,


I want to open a RowDetail of a GridView depending from one of its Item's value.

I've tried a foreach on every items but the GridViewRow returned with the helps of GridView.ItemContainerGenerator.ContainerFromItem(Item) is always null. The Item isn't null.

I've tried your solution but it didn't work for me.
visibleRow.Item is always null.
    
foreach (var visibleRow in this.myGridView.ChildrenOfType<GridViewRow>())
{
// this.itemsToExpand.Count>0!
if (this.itemsToExpand.Contains(visibleRow.Item)) // <-- visibleRow.Item is always null.
{
itemsToExpand.Remove(visibleRow.Item);
visibleRow.DetailsVisibility = Visibility.Visible;
visibleRow.Background =
new SolidColorBrush(Colors.Orange);
}
}

How can I open a single RowDetail by code from outside the grid.

Thanks,

Fred
0
Milan
Telerik team
answered on 28 Jan 2011, 04:32 PM

Hi Frédéric,

Is the detail that you are trying to open visible? If it is not you will not be able to find its row since RadGRidView would only generated UI element for the visible element only. One way to resolve this is to turn the vertical virtualization off by setting EnableRowVirtualization = false. Be ware that this might result in higher memory usage and slower start times.



Kind regards,
Milan
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
Frédéric
Top achievements
Rank 1
answered on 28 Jan 2011, 04:40 PM
Hi Milan,

I've managed to do what I expected this way:

In the Constructor I subscribed t this event:
mygrid.RowLoaded += new EventHandler<RowLoadedEventArgs>(mygrid_RowLoaded);

Then in the following method I get the row and its Item.
Then I check if its value equals zero and color its background in red and open the detail view.

void mygrid_RowLoaded(object sender, RowLoadedEventArgs e)
    {
        var row = e.Row as GridViewRow;
        if (row != null)
        {
            if (row.Item != null)
            {
                myObject o = (myObject)row.Item;
                if (o != null && o.Value == 0)
                {
                    row.Background = new SolidColorBrush(Colors.Red);
                    row.DetailsVisibility = Visibility.Visible;
                }
            }
        }
    }

Thanks for your help!

Fred
Tags
GridView
Asked by
Sumati
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Sumati
Top achievements
Rank 1
Pawel
Top achievements
Rank 1
Milan
Telerik team
Frédéric
Top achievements
Rank 1
Share this question
or