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

Index of last visible Row

8 Answers 949 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Thomas
Top achievements
Rank 1
Thomas asked on 21 Jul 2010, 10:58 AM
Hello,

is it possibly to get the index of the last visible row?

Regards,
Thomas

8 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 22 Jul 2010, 09:13 AM
Hello Thomas,

It was fun figuring this one out, you could try out the following code:
var rowHeight = this.radGridView1.TableElement.RowHeight;
var scrollPos = radGridView1.TableElement.RowScroller.Scrollbar.Value;
var groupHeight = this.radGridView1.TableElement.GroupHeaderHeight;
var headerHeight = this.radGridView1.TableElement.TableHeaderHeight;
var contentHeight = this.radGridView1.Height;
var posOfVisibleRow = (scrollPos + contentHeight - headerHeight - groupHeight) / rowHeight;
 
MessageBox.Show(this.radGridView1.Rows[posOfVisibleRow].Cells[0].Value.ToString());

This will work as long as all of the rows are the same height, but you should beware if a row is just partly visible it will not recognize it as being the last visible, i think you can figure it out from here.

The weirdest thing here is how you get the scrollbar position, in my tests the radGridView1.ScrollBar.Value always returned null, so i used the one inside the table element.

Please let me know if this worked.

Emanuel
0
Thomas
Top achievements
Rank 1
answered on 22 Jul 2010, 10:03 AM
Thanks for that, but you have it already said: it works as long the rows have the same size. i would prefer a solution for all cases (if it possibly).

Regards,
Thomas
0
Emanuel Varga
Top achievements
Rank 1
answered on 22 Jul 2010, 12:44 PM
Hello Thomas,

Try this, it should work with all kinds of rows, i hope i didn't forget anything, some fine tuning might still be required to suit your needs

private int GetLastRowIndex()
        {
            var rowHeight = this.radGridView1.TableElement.RowHeight;
            var scrollPos = radGridView1.TableElement.RowScroller.Scrollbar.Value;
             var groupHeight = 0;
            if (this.radGridView1.EnableGrouping)
                groupHeight = this.radGridView1.TableElement.GroupHeaderHeight;
             var headerHeight = this.radGridView1.TableElement.TableHeaderHeight;
             var contentHeight = this.radGridView1.Height;
             var spaceAvailable = scrollPos + contentHeight - headerHeight - groupHeight;
 
            if (this.radGridView1.AllowAddNewRow && this.radGridView1.AddNewRowPosition == SystemRowPosition.Top)
            {
                spaceAvailable -= rowHeight;
            }
 
            int pos = 0;
            while (true)
            {
                var actualRowHeight = this.radGridView1.Rows[pos].Height == -1 ? rowHeight : this.radGridView1.Rows[pos].Height;
                if (this.radGridView1.RowCount > pos && spaceAvailable - actualRowHeight > 0)
                {
                    pos++;
                    spaceAvailable -= actualRowHeight;
                }
                else
                {
                    break;
                }
            }
            return pos;
        }
0
Thomas
Top achievements
Rank 1
answered on 22 Jul 2010, 01:20 PM
Thanks! i will try this
0
Emanuel Varga
Top achievements
Rank 1
answered on 22 Jul 2010, 05:03 PM
Hello again,

I forgot about the case when you have just a small number of cells in the grid, less than the size of the grid and no scroll bars, here is the revised solution

private int GetLastRowIndex()
{
    var rowHeight = this.radGridView1.TableElement.RowHeight;
    var scrollPos = radGridView1.TableElement.RowScroller.Scrollbar.Value;
 
    var groupHeight = 0;
    if (this.radGridView1.EnableGrouping)
        groupHeight = this.radGridView1.TableElement.GroupHeaderHeight;
 
    var headerHeight = this.radGridView1.TableElement.TableHeaderHeight;
 
    var contentHeight = this.radGridView1.Height;
 
    var spaceAvailable = scrollPos + contentHeight - headerHeight - groupHeight;
 
    if (this.radGridView1.AllowAddNewRow && this.radGridView1.AddNewRowPosition == SystemRowPosition.Top)
    {
        spaceAvailable -= rowHeight;
    }
 
    int pos = 0;
    while (true)
    {
        if (this.radGridView1.RowCount - 1 <= pos)
        {
            break;
        }
 
        var actualRowHeight = this.radGridView1.Rows[pos].Height == -1 ? rowHeight : this.radGridView1.Rows[pos].Height;
        if (spaceAvailable - actualRowHeight > 0)
        {
            pos++;
            spaceAvailable -= actualRowHeight;
        }
        else
        {
            break;
        }
    }
 
    return pos;
}
0
Alexander
Telerik team
answered on 26 Jul 2010, 02:31 PM
Hello Thomas,

Emanuel, thank you for sharing your solution with the community. I have updated your Telerik points for your effort.

For a simpler solution you can examine the VisualRows collection:
this.radGridView1.TableElement.VisualRows

Basically, the last item in it is the last visible RadGridView row:
int visualRowsCount = this.radGridView1.TableElement.VisualRows.Count;
int lastIndex = this.radGridView1.TableElement.VisualRows[visualRowsCount - 1].RowInfo.Index;

Depending on the scenario it could be data, pinned or system row. Have this in mind when using it in your case.

Kind regards,
Alexander
the Telerik team
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 Public Issue Tracking system and vote to affect the priority of the items
0
scottw
Top achievements
Rank 1
answered on 01 Feb 2011, 11:08 PM
Is there a way to obtain this value from the collection of visual rows that will appear AFTER the scroll thumb has been moved?  I have a lot of data that needs to be loaded into a lazy loader and I need to obtain the range of rows that will be visible so I can load only the data that will be visible after someone drags the scroll bar. 

Thanks!
0
Alexander
Telerik team
answered on 04 Feb 2011, 05:04 PM
Hello Vania,

Thank you for your question.

In general, you can subscribe to the Scroll and ValueChanged events of the TableElement's VScrollBar to track the RadGridView scrollbar value changes. However, the VisualRows collection is updated asynchronously while scrolling and you cannot rely on these events to get the currently visual rows.

A possible solution for your scenario could be to use a Timer with Interval about 1s and to get the VisualRows collection on the Tick event of the timer.

I hope it helps.

Best regards,
Alexander
the Telerik team
Q3’10 SP1 of RadControls for WinForms is available for download; also available is the Q1'11 Roadmap for Telerik Windows Forms controls.
Tags
GridView
Asked by
Thomas
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Thomas
Top achievements
Rank 1
Alexander
Telerik team
scottw
Top achievements
Rank 1
Share this question
or