I have this project where the software communicates with a remote hardware.
Based on the visible rows, I need to read only the visible section inside a buffer in the hardware and update the grid accordingly (this is to prevent reading load of irrelevant data which is currently not visible to the user).
There is no easy way of telling what are the visible rows in the GridView (I have read that there was, but now we have to dig in).
I have tried using the method in the example that was given here:
http://www.telerik.com/community/forums/wpf/gridview/firstvisiblechildindex.aspx
However, for some reason in my project it will never find the last row, therefore causing an exception.
I have used a different method of getting all the rows from the virtualizing stack panel and then iterating them to see what is the lowest item index and using the rows count at the num of rows:
This does work (although it seems to return a little more rows than expected when the scrollviewer is visible....
Still, when the UI starts I try getting the visible rows on several occasions:
radGridView_Loaded
radGridView_DataLoaded
and also: radGridView_ScrollChanged
The problem is that it is not enough...
When I get the DataLoaded event, the rowsList is still empty... I need to wait for the next LayoutUpdated event and only then call the
If I try to rely only on the ScrollChanged event then if there are for example 10 rows and the window are can contain 20 row, then if I add or remove rows then this event is never being called.
I did not find any way to get notified when the visible rows change. Is there any solution?
Thanks.
Based on the visible rows, I need to read only the visible section inside a buffer in the hardware and update the grid accordingly (this is to prevent reading load of irrelevant data which is currently not visible to the user).
There is no easy way of telling what are the visible rows in the GridView (I have read that there was, but now we have to dig in).
I have tried using the method in the example that was given here:
http://www.telerik.com/community/forums/wpf/gridview/firstvisiblechildindex.aspx
However, for some reason in my project it will never find the last row, therefore causing an exception.
I have used a different method of getting all the rows from the virtualizing stack panel and then iterating them to see what is the lowest item index and using the rows count at the num of rows:
private void recalculateFirstAndLastVisibleIndexes()
{
var gvvp = this.radGridView.ChildrenOfType<
GridViewVirtualizingPanel
>().FirstOrDefault();
if (gvvp == null)
return;
var rowsList = gvvp.ChildrenOfType<
GridViewRow
>();
if (rowsList.Any())
{
int firstIndex = this.radGridView.Items.IndexOf(rowsList.ElementAt(0).Item);
foreach (var row in rowsList)
{
firstIndex = Math.Min(firstIndex, this.radGridView.Items.IndexOf(row.Item));
}
FirstVisibleRowIndex = firstIndex;
NumOfVisibleRows = rowsList.Count();
}
else
{
FirstVisibleRowIndex = 0;
NumOfVisibleRows = 0;
}
}
This does work (although it seems to return a little more rows than expected when the scrollviewer is visible....
Still, when the UI starts I try getting the visible rows on several occasions:
radGridView_Loaded
radGridView_DataLoaded
and also: radGridView_ScrollChanged
The problem is that it is not enough...
When I get the DataLoaded event, the rowsList is still empty... I need to wait for the next LayoutUpdated event and only then call the
recalculateFirstAndLastVisibleIndexes
function.If I try to rely only on the ScrollChanged event then if there are for example 10 rows and the window are can contain 20 row, then if I add or remove rows then this event is never being called.
I did not find any way to get notified when the visible rows change. Is there any solution?
Thanks.