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

Gridview Visible Row Count

6 Answers 1406 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Raghuprasad
Top achievements
Rank 1
Raghuprasad asked on 15 Dec 2011, 12:26 PM
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;
            }

6 Answers, 1 is accepted

Sort by
0
Mark
Top achievements
Rank 1
answered on 19 Dec 2011, 05:29 AM
try this:

int rowCount = myGridView.ChildRows.Count(row => row.IsVisible);
0
Raghuprasad
Top achievements
Rank 1
answered on 19 Dec 2011, 07:21 AM
Hey Mark!!!

Thank you. It really helped to solve my issue.
0
erwin
Top achievements
Rank 1
Veteran
Iron
answered on 19 Dec 2011, 03:01 PM
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
0
Jack
Telerik team
answered on 20 Dec 2011, 11:49 AM
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.

0
erwin
Top achievements
Rank 1
Veteran
Iron
answered on 20 Jan 2012, 03:48 PM
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
0
ustr
Top achievements
Rank 1
answered on 13 Mar 2014, 07:38 AM
Hello,
try this:

return gridView.TableElement.VisualRows.OfType<GridDataRowElement>().Count();
Tags
GridView
Asked by
Raghuprasad
Top achievements
Rank 1
Answers by
Mark
Top achievements
Rank 1
Raghuprasad
Top achievements
Rank 1
erwin
Top achievements
Rank 1
Veteran
Iron
Jack
Telerik team
ustr
Top achievements
Rank 1
Share this question
or