Gridview Visible Row Count

7 posts, 0 answers
  1. Raghuprasad
    Raghuprasad avatar
    2 posts
    Member since:
    Aug 2011

    Posted 15 Dec 2011 Link to this post

    I am using rad gridview for my winforms. Here i am showing n number of rows in the grid (say for example i have 10 rows), based on some condition i am making few rows invisible to the user (i.e. out of 10 i am displaying 3).

    Is there way to get row count which are visible to user?

     IEnumerator gridRowEnumerator = RadGridView1.Rows.GetEnumerator();
                while (gridRowEnumerator.MoveNext())
                {

                    GridViewRowInfo gridViewRowInfo = (GridViewRowInfo)(gridRowEnumerator.Current);

                    bool found = false;
                    if (searchBox.Text == "") // if blank then all rows are visible.
                    {
                        found = true;
                    }
                    else
                    {
                        foreach (GridViewCellInfo g in gridViewRowInfo.Cells)
                        {
                            if (g.Value != null && g.Value.ToString().ToLower().Contains(searchBox.Text.ToLower()))
                            {
                                found = true;
                                break;
                            }
                        }
                    }

                    gridViewRowInfo.IsVisible = found;
                }
  2. Mark
    Mark avatar
    8 posts
    Member since:
    Jun 2011

    Posted 18 Dec 2011 Link to this post

    try this:

    int rowCount = myGridView.ChildRows.Count(row => row.IsVisible);
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Raghuprasad
    Raghuprasad avatar
    2 posts
    Member since:
    Aug 2011

    Posted 19 Dec 2011 Link to this post

    Hey Mark!!!

    Thank you. It really helped to solve my issue.
  5. erwin
    erwin avatar
    358 posts
    Member since:
    Dec 2006

    Posted 19 Dec 2011 Link to this post

    Be aware that if you allow grouping in the grid, you may have to recursively traverse the ChildRows Collection of each group.
    See my earlier post about this.

    Regards
    Erwin
  6. Jack
    Admin
    Jack avatar
    2335 posts

    Posted 20 Dec 2011 Link to this post

    Hello Raghuprasad,

    I am glad to hear that you managed to solve this issue. 

    In addition to the offered options, you can use the following code to get all visible data rows in RadGridView:
    int visibleDataRows = 0;
    GridTraverser traverser = new GridTraverser(this.radGridView1.MasterView);
    while (traverser.MoveNext())
    {
        if (traverser.Current is GridViewDataRowInfo)
        {
            visibleDataRows++;
        }
    }

    This code should work faster when using a large number of items.

    However, if you want to get the count of all data rows that are currently visible on screen, you should use the following code:
    int currentlyVisibleDataRows = 0;
    foreach (GridRowElement row in this.radGridView1.TableElement.VisualRows)
    {
        if (row is GridDataRowElement)
        {
            currentlyVisibleDataRows++;
        }
    }

    I hope this helps.
     
    Best wishes,
    Jack
    the Telerik team

    Q3’11 of RadControls for WinForms is available for download (see what's new). Get it today.

  7. erwin
    erwin avatar
    358 posts
    Member since:
    Dec 2006

    Posted 20 Jan 2012 Link to this post

    Hi Jack,

    your code is not equivalent to mine in case grouping is used. It boils down to the definition of "visible". In my case it's visible versus filtered out while  your code provides the number of rows visible in the grid. In case of collapsed groups I count the data rows as logically visible, even if they are not visible on screen.

    Regards
    Erwin
  8. ustr
    ustr avatar
    4 posts
    Member since:
    May 2011

    Posted 13 Mar 2014 Link to this post

    Hello,
    try this:

    return gridView.TableElement.VisualRows.OfType<GridDataRowElement>().Count();
Back to Top
UI for WinForms is Visual Studio 2017 Ready