2

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:

private List<object> ReturnListFounds(string text)
        {
            List<object> a = new List<object>();
            foreach (var item in myGrid.Items)
            {
                if (item == null)
                    continue;
                GridViewRow row = myGrid.ItemContainerGenerator.ContainerFromItem(item) as GridViewRow;

                if (row == null)
                    continue;

                foreach (GridViewCell cell in row.Cells)
                {
                    if (cell != null && cell.Value != null)
                    {
                        string str = cell.Value.ToString();

                        if (str.Equals(text, StringComparison.InvariantCultureIgnoreCase) || str.ToLower().Contains(text.ToLower()))
                        {
                            a.Add(row.Item);
                            break;
                        }
                    }
                }
            }

            return a;
        }

@Edit

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?

Evil Str
  • 132
  • 9
  • Why do you need to iterate through VisualTree items? – Spawn Nov 24 '15 at 19:29
  • I want to get all the cells from the grid... I need its value to compare to a String and also I need the cells to do another thing. But, the most important it's to get the value to compare to a String. – Evil Str Nov 25 '15 at 11:54

2 Answers2

3

Have you tried this?

var rows = StrategyGridView.ChildrenOfType<GridViewRow>();

It works fine for me. Hope it helps!

ltiveron
  • 579
  • 7
  • 16
0

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!

private List<object> ReturnListFounds(string text)
        {
            List<object> result = new List<object>();

            for (int l = 0; l <= Items.Count; l++)
            {
                var cell = new GridViewCellInfo(this.Items[l], this.Columns[0], this);

                if (cell.Item != null)
                {
                    var props = cell.Item.GetType().GetProperties();

                    foreach (var p in props)
                    {
                        if (p == null || cell.Item == null)
                            continue;

                        var t = p.GetValue(cell.Item);

                        if (t == null)
                            continue;

                        var str = t.ToString();
                        if (str.Equals(text, StringComparison.InvariantCultureIgnoreCase) || str.ToLower().Contains(text))
                        {
                            result.Add(cell.Item);
                        }

                    }
                }

            }

            result = new List<object>(result.Distinct());

            return result;
        }
Evil Str
  • 132
  • 9
  • You have some DataContext for RadGridView, why you don't iterate through it? – Spawn Nov 25 '15 at 13:04
  • I tried. But I am styling this grid, so the code isn't in a class on my main project. It is on a class that is connected to a ResourceDictionary and i am styling the grid as the way I want to... So I couldn't work with the DataContext. I don't know if there is a way of doing this, but I couldn't. – Evil Str Nov 25 '15 at 13:11
  • Well, it's not for WPF, wrong approach. As I understand, you don't use MVVM pattern. – Spawn Nov 25 '15 at 13:22