DataGridView无意中捕捉

时间:2017-11-01 12:43:18

标签: c# datagridview scrollbar

如果因为我的英语不好而难以理解我的问题,我会提前道歉。

在C#中的Windows窗体应用程序中,我有一个DataGridView控件,可以显示最新的表数据。使用Timer控件,它会自动刷新DataGridView中的表格。 由于列中的列数多于DataGridView可以同时显示的列数,因此有一个horizontal scrollbar

我也使用FirstDisplayedScrollingColumnIndex来获取当前滚动位置。这样,当用户使用scrollbar浏览表并发生表刷新时,scrollbar可以保持在刷新之前的位置。

然而,有一个问题。由于FirstDisplayedScrollingColumnIndex只能存储整数值,因此当视图中表格最左侧的列未完全显示时,它会在表刷新时快照,以便最左边的列<视图中的em> 完全显示。当用户使用scrollbar浏览最右边的列时,这会成为问题,因为在刷新时,它会快速显示最左边的列,而不是最右边的列,即scrollbar不会停留在最右边的位置。

这是图片。这是应该继续在表刷新时显示的内容: enter image description here

如您所见,列已完全显示,但最左侧的列显示在中途。当表刷新发生时,它会向后捕捉一列以显示视图中最左侧的列 。这可以防止用户在刷新时查看Sex列的数据。 (下图)我认为这是因为FirstDisplayedScrollingColumnIndex存储一个整数值,即列位置。

enter image description here

private int scrollPositionColumn;
private void Timer_Tick(object sender, EventArgs e){
    //Before the update, save the current scrollbar position
    if(MyDGV.FirstDisplayedScrollingColumnIndex >= 0){
        scrollPositionColumn = MyDGV.FirstDisplayedScrollingColumnIndex;
    }
    UpdateGridView();
}

private void UpdateGridView(){
    /*Some code*/

    //Restore the previous position of the scrollbar
    MyDGV.FirstDisplayedScrollingColumnIndex = scrollPositionColumn;
    /*Some code*/
}

有人可以就如何解决这个问题给我建议吗?

这是我的简化代码。

0 个答案:

没有答案