当数据更改时,如何在 DataGrid 中保持选择? [英] How do I maintain selection in a DataGrid when the data changes?

查看:33
本文介绍了当数据更改时,如何在 DataGrid 中保持选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DataGrid,我将 DataProvider 设置为我的数据.当我的数据更改时,DataGrid 丢失所选行并且滚动条跳回到顶部.如何保持选择和滚动位置?

I have a DataGrid and I set the DataProvider to my data. When my data changes the DataGrid loses the selected row and the scroll bar jumps back to the top. How do I maintain the selection and scroll position?

推荐答案

如果你只是想保持位置:

If you just want to maintain position:

在任何改变数据的函数中,首先捕获选定的索引

in whatever function is changing the data, first capture the selected index

var myidx:int = new int(myDG.selectedIndex);

和滚动位置

var myVertPos:int = new int(myDG.verticalScrollPosition);

运行更改数据的代码,然后向后执行上述步骤:

run the code that changes the data then do the above steps backwards:

myDG.selectedIndex = myidx;
myDG.verticalScrollPosition = myVertPos;

哦,您可能需要进行检查以确保所选索引不超过 DG 中项目的长度,如果超过则选择最后一个.根据我的经验,将垂直滚动位置设置为大于最大值只会导致滚动到最大值.

Oh and you will probably want to do a check to make sure that the selected index is not over the length of items now in your DG and select the last one if it is. In my experience setting the vertical scroll position greater than the max just results in scrolling to the max.

这篇关于当数据更改时,如何在 DataGrid 中保持选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆