WPF - RadGridView循环遍历行。无法获取所有行

时间:2015-11-24 13:49:59

标签: c# wpf telerik radgridview

我正在尝试迭代我的RadGridView行,但是当我有超过20或30个项目时,循环不会获得所有行。

例如:在带有5个项目的radgridview中使用此代码,我可以获得所有这些代码并执行我想要的任何操作,但是当我的网格有超过20个项目时,它只能获得10行。这是一个bug还是类似的东西?我该如何解决?

这是我的代码:

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

我发现了问题。问题是:如果项目在视图区域之外,方法“ItemContainerGenerator.ContainerFromItem(item)as GridViewRow”将返回null。但我在包含123项的网格中使用此方法,我只能得到20个第一项的行。 我需要能够获得所有项目,而不仅仅是视图区域中的项目。我已经尝试将虚拟化设置为false(EnableRowVirtualization = false; EnableColumnVirtualization = false;),但它也不起作用。

有没有办法使用这种方法获取所有行?

2 个答案:

答案 0 :(得分:1)

你试过这个吗?

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

对我来说很好。希望它有所帮助!

答案 1 :(得分:0)

我尝试了很多东西来完成这项工作,我找到了一个。这不是最好的方法,但它确实有效。我有谁有更好的,只是发布在这里!与我们分享!

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;
        }
相关问题