当数据源更改时,如何防止 DataGridView 自动滚动?

发布于 2024-08-06 15:37:50 字数 442 浏览 3 评论 0原文

我尝试了这个(http:// Brainof-dave.blogspot.com/2007/08/turning-off-auto-scrolling-in-bound.html) 在作为 DataGridView 的数据源的 DataTable 上的“RowChanged”事件中,但没有用。

基本上,我有一个带有 BindingSource 的 DataGridView,因为它是 DataSource。 BindingSource 的DataSource 是一个包含DataTable 的DataView。每当其中一行中的数据发生更改时,DataGridView 就会滚动回顶部。有一个简单的解决办法吗?

I tried this (http://brainof-dave.blogspot.com/2007/08/turning-off-auto-scrolling-in-bound.html) in the "RowChanged" event on the DataTable that is the data source for the DataGridView, but to no avail.

Basically, I have a DataGridView with a BindingSource as it's DataSource. The BindingSource's DataSource is a DataView that contains a DataTable. Every time data in one of the rows changes, the DataGridView scrolls back to the top. Is there a simple fix for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

当梦初醒 2024-08-13 15:37:50

看起来我找到了它:http://seewinapp。 blogspot.com/2005/09/is-your-autoscroll-too-auto.html

我覆盖了 DataTable 上的 RowChanged 事件,存储了 FirstDisplayedScrollingRowIndex,使用该索引作为参数调用委托方法,然后重置委托方法内该参数的 FirstDisplayedScrollingRowIndex。事实证明,直到所有事件都被触发后,自动滚动才会发生,因此尝试在事件中破解它是没有用的。该委托之所以有效,是因为它是在事件发生后调用的。

Looks like I found it: http://seewinapp.blogspot.com/2005/09/is-your-autoscroll-too-auto.html

I overrode the RowChanged event on the DataTable, stored the FirstDisplayedScrollingRowIndex, invoked a delegate method with that index as the argument, and then reset the FirstDisplayedScrollingRowIndex to that argument inside the delegate method. It turns out that auto-scroll doesn't occur until after all events have been fired, so it's useless to try to hack it inside an event. The delegate works because it is invoked after the events.

别理我 2024-08-13 15:37:50

这是经过测试的代码,可在更改数据源后恢复 RowIndex。这还会恢复排序顺序和最后一个单元格位置。语言:C#7.0。
这是我在网络搜索的帮助下亲自编写的代码。

    private void UpdateDataSource()
    {
        SuspendLayout();

        //Save last position and sort order
        DataGridView g = DataGridView1;
        Int32 idxFirstDisplayedScrollingRow = g.FirstDisplayedScrollingRowIndex;
        SortOrder dgvLastSortDirection = g.SortOrder;
        Int32 lastSortColumnPos = g.SortedColumn?.Index ?? -1;
        Int32 dgvLastCellRow = g.CurrentCell?.RowIndex ?? -1;
        Int32 dgvLastCellColumn = g.CurrentCell?.ColumnIndex ?? -1;

        //Set new datasource
        g.DataSource = myNewDataTableSource;                                                                     

        //Restore sort order, scroll row, and active cell
        g.InvokeIfRequired( o =>
        {
            if(lastSortColumnPos > -1)
            {
                DataGridViewColumn newColumn = o.Columns[lastSortColumnPos];
                switch(dgvLastSortDirection)
                {
                    case SortOrder.Ascending:
                        o.Sort(newColumn, ListSortDirection.Ascending);
                        break;
                    case SortOrder.Descending:
                        o.Sort(newColumn, ListSortDirection.Descending);
                        break;
                    case SortOrder.None:
                        //No sort
                        break;
                }
            }

            if(idxFirstDisplayedScrollingRow >= 0)
                o.FirstDisplayedScrollingRowIndex = idxFirstDisplayedScrollingRow;

            if(dgvLastCellRow>-1 && dgvLastCellColumn>-1)
                o.CurrentCell = g[dgvLastCellColumn, dgvLastCellRow];
        } );

        ResumeLayout();
    }

    public static void InvokeIfRequired<T>(this T obj, InvokeIfRequiredDelegate<T> action) where T : ISynchronizeInvoke
    {
        if (obj.InvokeRequired)
        {
            obj.Invoke(action, new Object[] { obj });
        }
        else
        {
            action(obj);
        }
    } 

Here is tested code that restores the RowIndex after changing the datasource. This also restores the sort order and last cell position. Language: C# 7.0.
This is code I wrote personally, with some help from web searches.

    private void UpdateDataSource()
    {
        SuspendLayout();

        //Save last position and sort order
        DataGridView g = DataGridView1;
        Int32 idxFirstDisplayedScrollingRow = g.FirstDisplayedScrollingRowIndex;
        SortOrder dgvLastSortDirection = g.SortOrder;
        Int32 lastSortColumnPos = g.SortedColumn?.Index ?? -1;
        Int32 dgvLastCellRow = g.CurrentCell?.RowIndex ?? -1;
        Int32 dgvLastCellColumn = g.CurrentCell?.ColumnIndex ?? -1;

        //Set new datasource
        g.DataSource = myNewDataTableSource;                                                                     

        //Restore sort order, scroll row, and active cell
        g.InvokeIfRequired( o =>
        {
            if(lastSortColumnPos > -1)
            {
                DataGridViewColumn newColumn = o.Columns[lastSortColumnPos];
                switch(dgvLastSortDirection)
                {
                    case SortOrder.Ascending:
                        o.Sort(newColumn, ListSortDirection.Ascending);
                        break;
                    case SortOrder.Descending:
                        o.Sort(newColumn, ListSortDirection.Descending);
                        break;
                    case SortOrder.None:
                        //No sort
                        break;
                }
            }

            if(idxFirstDisplayedScrollingRow >= 0)
                o.FirstDisplayedScrollingRowIndex = idxFirstDisplayedScrollingRow;

            if(dgvLastCellRow>-1 && dgvLastCellColumn>-1)
                o.CurrentCell = g[dgvLastCellColumn, dgvLastCellRow];
        } );

        ResumeLayout();
    }

    public static void InvokeIfRequired<T>(this T obj, InvokeIfRequiredDelegate<T> action) where T : ISynchronizeInvoke
    {
        if (obj.InvokeRequired)
        {
            obj.Invoke(action, new Object[] { obj });
        }
        else
        {
            action(obj);
        }
    } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文