Get visible rows returns more rows than visible

7 posts, 0 answers
  1. Benedikt
    Benedikt avatar
    19 posts
    Member since:
    Apr 2014

    Posted 22 Oct 2015 Link to this post

    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!

  2. Stefan Nenchev
    Admin
    Stefan Nenchev avatar
    281 posts

    Posted 26 Oct 2015 Link to this post

    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 article 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
  3. UI for WPF is Visual Studio 2017 Ready
  4. Benedikt
    Benedikt avatar
    19 posts
    Member since:
    Apr 2014

    Posted 26 Oct 2015 in reply to Stefan Nenchev Link to this post

    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".
  5. Benedikt
    Benedikt avatar
    19 posts
    Member since:
    Apr 2014

    Posted 26 Oct 2015 in reply to Stefan Nenchev Link to this post

    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;
        }
    }

  6. Stefan Nenchev
    Admin
    Stefan Nenchev avatar
    281 posts

    Posted 28 Oct 2015 Link to this post

    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
  7. Benedikt
    Benedikt avatar
    19 posts
    Member since:
    Apr 2014

    Posted 01 Nov 2015 Link to this post

    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;
        }
    }

  8. Stefan Nenchev
    Admin
    Stefan Nenchev avatar
    281 posts

    Posted 02 Nov 2015 Link to this post

    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
Back to Top
UI for WPF is Visual Studio 2017 Ready