RadGridView loop through rows. Can't get all rows

6 posts, 0 answers
  1. Evil
    Evil avatar
    18 posts
    Member since:
    Mar 2015

    Posted 24 Nov 2015 Link to this post

    I'm trying to iterate through my RadGridView rows, but when I have more than 20 or 30 items, the loop doesn't get all rows.
    For example: using this code in a radgridview with 5 items, I can get all of them and do whatever I want, but when my grid has more than 20 items, it gets only 10 rows. Is this a bug or something like that? How can I solve it?
    Here's my code:

     

    01.private List<object> ReturnListFounds(string text)
    02.        {
    03.            List<object> a = new List<object>();
    04.            foreach (var item in myGrid.Items)
    05.            {
    06.                if (item == null)
    07.                    continue;
    08.                GridViewRow row = myGrid.ItemContainerGenerator.ContainerFromItem(item) as GridViewRow;
    09. 
    10.                if (row == null)
    11.                    continue;
    12. 
    13.                foreach (GridViewCell cell in row.Cells)
    14.                {
    15.                    if (cell != null && cell.Value != null)
    16.                    {
    17.                        string str = cell.Value.ToString();
    18. 
    19.                        if (str.Equals(text, StringComparison.InvariantCultureIgnoreCase) || str.ToLower().Contains(text.ToLower()))
    20.                        {
    21.                            a.Add(row.Item);
    22.                            break;
    23.                        }
    24.                    }
    25.                }
    26.            }
    27. 
    28.            return a;
    29.        }

  2. Evil
    Evil avatar
    18 posts
    Member since:
    Mar 2015

    Posted 24 Nov 2015 in reply to Evil Link to this post

    I found out the problem. The thing is: the method "ItemContainerGenerator.ContainerFromItem(item) as GridViewRow" returns null if the item is outside of the view area. But I'm using this method in a grid containing 123 items and I can only get the row for the 20 first items.
    I need to be able to get all of the items, not just the ones in the view area. I have already tried to set the virtualization false (EnableRowVirtualization = false; EnableColumnVirtualization = false;), but it didin't work as well.

    Is there a way of getting all of the rows using this method?
  3. UI for WPF is Visual Studio 2017 Ready
  4. Evil
    Evil avatar
    18 posts
    Member since:
    Mar 2015

    Posted 25 Nov 2015 Link to this post

    I tried a lot of things to make this work and I found one. It's not the best way of doing this, but it works. I anyone has anything better, just post here! Share with us!

     

    01.private List<object> ReturnListFounds(string text)
    02.        {
    03.            List<object> result = new List<object>();
    04. 
    05.            for (int l = 0; l <= Items.Count; l++)
    06.            {
    07.                var cell = new GridViewCellInfo(this.Items[l], this.Columns[0], this);
    08. 
    09.                if (cell.Item != null)
    10.                {
    11.                    var props = cell.Item.GetType().GetProperties();
    12. 
    13.                    foreach (var p in props)
    14.                    {
    15.                        if (p == null || cell.Item == null)
    16.                            continue;
    17. 
    18.                        var t = p.GetValue(cell.Item);
    19. 
    20.                        if (t == null)
    21.                            continue;
    22. 
    23.                        var str = t.ToString();
    24.                        if (str.Equals(text, StringComparison.InvariantCultureIgnoreCase) || str.ToLower().Contains(text))
    25.                        {
    26.                            result.Add(cell.Item);
    27.                        }
    28. 
    29.                    }
    30.                }
    31. 
    32.            }
    33. 
    34.            result = new List<object>(result.Distinct());
    35. 
    36.            return result;
    37.        }

    I tried a lot of things to make this work and I found one. It's not the best way of doing this, but it works. I anyone has anything better, just post here! Share with us!

    I tried a lot of things to make this work and I found one. It's not the best way of doing this, but it works. I anyone has anything better, just post here! Share with us!

  5. Dilyan Traykov
    Admin
    Dilyan Traykov avatar
    370 posts

    Posted 25 Nov 2015 Link to this post

    Hello Evil,

    Generally we do not recommend working with the visual elements (such as GridViewRow) as RadGridView is a virtualized control and its elements are reused as they go in and out the visual area. You can check the topic on UI virtualization for further information. We also don't recommend disabling the virtualization as it can lead to degraded performance.

    My suggestion would be to simply cast the gridView.Items to a custom class containing all of the columns as properties and iterate over them. I see no reason for the item to be cast to a GridViewRow.

    As the effect, you're trying to achieve resembles filtering I would also suggest having a look at the Programmatic Filtering article in the RadGridView documentation. There are also some good examples in our SDK Examples Browser.

    Regards,
    Dilyan Traykov
    Telerik
    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 Feedback Portal and vote to affect the priority of the items
  6. Naresh
    Naresh avatar
    3 posts
    Member since:
    Apr 2016

    Posted 05 May in reply to Dilyan Traykov Link to this post

    Hi Dilyan,

    I using the telerik:RadGridView control in my app, my requirement is to add search box(i did it as wpf custom usercontrol, not the searchbox control of telerik). my search control has find next & find prev command. When user enters any char or string in this searchbox the result is shown in radgridview by changing the color of the text in the radgridview cells. everything is fine upto now.

    when i hit the find next command i had added code to navigate to next text matching cell and highlighted the text in that cell. but when the row's are out of the radgridview viewable area then the ItemContainterGenerator.ContainerfromItem() returns null. what shall i do to get the valid GridviewRow. So here when i get null then iam scroll that item into view with ScrollToView(item), respective row gets scrolled to view but after that if i try to get the GridViewRow of that item iam still getting null.

    The samples does not cover my situation.

    Please reply ASAP, 

    Thanks in Advance for your reply.

  7. Dilyan Traykov
    Admin
    Dilyan Traykov avatar
    370 posts

    Posted 09 May Link to this post

    Hello Naresh,

    The behavior you're observing is due to the fact that the GridViewRow has not been loaded once the ContainerFromItem() method is invoked. As suggested in my previous reply, working directly with the visual elements is not recommended. Please bear that in mind and try to use another approach if possible. I can suggest using a RowStyleSelector or CellStyleSelector to conditionally style the desired elements.

    Regards,
    Dilyan Traykov
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Back to Top
UI for WPF is Visual Studio 2017 Ready