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

Get visible rows returns more rows than visible

6 Answers 745 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Benedikt
Top achievements
Rank 1
Benedikt asked on 22 Oct 2015, 10:01 AM

Currently i have the problem, that i need to know how much visible rows are in the grid. For this, i use the following code:

 

return this.ChildrenOfType<GridViewRow>().Where(item => item.IsVisible).Count();

But this always returns more columns (about 2 - 5) than i can really see. So what am i doing wrong?

 Thank you very much!

6 Answers, 1 is accepted

Sort by
0
Stefan Nenchev
Telerik team
answered on 26 Oct 2015, 02:54 PM
Hello Benedikt,

The reason behind the inconsistencies of the data is due to the UI virtualization of the RadGridView and the GridViewRow being a visual element. We do not advise on working with such elements on the code behind as it could result in handling wrong information, as in your case. Instead, you should work with the data source collection directly. On the following you can get more information regarding the UI Virtualization

What could be helpful in your situation is the following forum thread - FirstVisibleChildIndex.  Please, check the approach suggested there and consider whether implementing it in your project would work. 

If this doesn't fit your needs, please share additional details on your end goal (why you need to know the count of visible rows in the view) and we will try to suggest an alternative solution.

Regards,
Stefan Nenchev
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
0
Benedikt
Top achievements
Rank 1
answered on 26 Oct 2015, 02:56 PM
Thank you for your answer. My goal is to implement my own Page-Up and Page-Down functionality, because we need very specific behaviour. For this reason i have to know, how much large is a "page".
0
Benedikt
Top achievements
Rank 1
answered on 26 Oct 2015, 03:08 PM

As an additional informat, your solution with: GetLastVisibleIndexFromAllRealized does not work. I get totally rendom numbers, by using it like this: 

public int VisibleRowCount
{
    get
    {
        var res = GetLastVisibleIndexFromAllRealized() - GetFirstVisibleIndexFromAllRealized();
 
        if(res < 0)
        {
            res = 0;
        }
 
        return res;
    }
}

0
Stefan Nenchev
Telerik team
answered on 28 Oct 2015, 04:25 PM
Hi Benedikt,

I am sorry to hear that the project from the other forum thread was not relevant in your situation. As advised in the thread itself, this implementation is kind of a hack and was designed to work in just one certain scenario. 

After some further investigation from our side, what I can suggest for you is to use the GridViewVirtualizingPanel in order to get the page height. Please refer to the following code snippet:

var viewportHeight = this.radGridView.ChildrenOfType<GridViewVirtualizingPanel>().FirstOrDefault().ViewportHeight;
.
Eventually, you can use it in order to get the number of the visible rows as well:

var visibleRows = viewportHeight / this.radGridView.RowHeight;

Please, update me if this information was of help.


Regards,
Stefan Nenchev
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
1
Benedikt
Top achievements
Rank 1
answered on 01 Nov 2015, 05:09 PM

This seems to solve the problem

public int VisibleRowCount
{
    get
    {
        int visibleRows = 0;
 
        var viewportHeight = this.ChildrenOfType<GridViewVirtualizingPanel>().FirstOrDefault().ViewportHeight;
        double height = 0;
 
        foreach (var row in this.ChildrenOfType<GridViewRow>().Where(item => item.IsVisible))
        {
            height += row.ActualHeight;
 
            if (height <= viewportHeight)
            {
                visibleRows++;
            }
            else
            {
                break;
            }
        }
         
        return visibleRows;
    }
}

0
Stefan Nenchev
Telerik team
answered on 02 Nov 2015, 03:21 PM
Hi Benedikt,

I am happy to see that the implementation was useful in your case. 

Regards,
Stefan Nenchev
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
Tags
GridView
Asked by
Benedikt
Top achievements
Rank 1
Answers by
Stefan Nenchev
Telerik team
Benedikt
Top achievements
Rank 1
Share this question
or