在 DataGridView 中排序时等待光标

发布于 2024-07-08 01:44:46 字数 132 浏览 8 评论 0原文

我正在使用标准的.Net 2.0 DataGridView,列上的排序模式为自动。 它非常非常慢(这可能是关于如何加快速度的另一个问题),但我似乎找不到在执行排序操作时维护 WaitCursor 的事件或事件组合。

有想法吗?

I am using the standard .Net 2.0 DataGridView with sort mode of automatic on the column. It is very very slow (which should probably be another question on how to speed it up) but I can't seem to find an event or combination of events that will maintain a WaitCursor while this sort operation is being performed.

Ideas?

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

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

发布评论

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

评论(2

山田美奈子 2024-07-15 01:44:46

挂钩“MouseDown”事件,用“HitTest”方法检查用户点击的位置。 如果用户单击列标题,则设置“Cursor.Current = Cursors.Wait”并将某些标志“isSorting”设置为 true。

挂钩“MouseUp”事件,检查“isSorting”标志是否为true,然后设置“Cursor.Current = Cursors.Default”。

编辑:
使用“Sorted”事件而不是“MouseUp”。
“MouseUp”是功能性的,但“Sorted”是“更干净”的解决方案。

Hook "MouseDown" event, check with "HitTest" method where user clicked. If user clicked on the column header set "Cursor.Current = Cursors.Wait" and set some flag "isSorting" to true.

Hook "MouseUp" event, check if "isSorting" flag true, then set "Cursor.Current = Cursors.Default".

EDIT:
Use "Sorted" event instead of "MouseUp".
The "MouseUp" is funtionaly, but the "Sorted" is "cleaner" solution.

心房的律动 2024-07-15 01:44:46

《纪元 2021》,死灵术是最好的:)我仍在寻找一种方法来正确地做到这一点。

我尝试过的方法和无效的方法

当用户开始拖动(重新排序列标题)时,也会触发 CellMouseDown 事件。 所以这是一个妥协。

CellMouseClick 事件在 Sorted 事件之后触发。 由于线程在排序操作期间被占用并且没有真正的 SortStart 事件,我开始认为这是不可能的。

我最终做了什么

如果您使用 DataTable 作为 gridview 的数据源,则必须禁用每列的自动排序。

foreach (DataGridViewColumn column in dgvUsers.Columns)
{
    column.SortMode = DataGridViewColumnSortMode.Programmatic;
}

你也许可以这样做
我最终使用 CellMouseDown 事件、CellMouseUp 事件和两个变量 mouseDownColumnIndexsortExpression 作为先前排序的句柄行动。

    string mouseDownColumnName = "";
    string sortExpression = "";
    private void dgvUsers_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
    {
        mouseDownColumnName = dgvUsers.Columns[e.ColumnIndex].HeaderText;
    }

    private void dgvUsers_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left // Obviously
            && e.RowIndex == -1 // column header row index
            && mouseDownColumnName == dgvUsers.Columns[e.ColumnIndex].HeaderText // No drag gesture
        ) {
            dgvUsers.Cursor = Cursors.WaitCursor;
            Task<DataTable> sortAction = Task<DataTable>.Factory.StartNew(() =>
            {
                DataView dvUsers = ((DataTable)dgvUsers.DataSource).DefaultView;
                string headerText = dgvUsers.Columns[e.ColumnIndex].HeaderText;
                if (sortExpression == $"{headerText} asc")
                    dvUsers.Sort = $"{headerText} desc";
                else
                    dvUsers.Sort = $"{headerText} asc";
                sortExpression = dvUsers.Sort;
                return dvUsers.ToTable();
            });
            sortAction.Wait();
            dgvUsers.DataSource = sortAction.Result;

            dgvUsers.Cursor = Cursors.Default;
        }
    }

这似乎比默认的“自动”排序更快。

Anno 2021, necromancy at it's best :) I was still looking for a way to do this correctly.

What I tried and what didn't work

The CellMouseDown event is also fired when a user starts dragging (reordering column headers). So that's a compromise.

The CellMouseClick event is fired AFTER the Sorted event. Since the thread is occupied during the sorting operation and there is no true SortStart event, I was starting to think it would be impossible.

What I ended up doing

If you're using a DataTable as a datasource for your gridview, you'll have to disable automatic sorting for each column.

foreach (DataGridViewColumn column in dgvUsers.Columns)
{
    column.SortMode = DataGridViewColumnSortMode.Programmatic;
}

You might be able to do this with
I ended up using the CellMouseDown event, the CellMouseUp event and two variables mouseDownColumnIndex and sortExpression as handles on previous sorting actions.

    string mouseDownColumnName = "";
    string sortExpression = "";
    private void dgvUsers_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
    {
        mouseDownColumnName = dgvUsers.Columns[e.ColumnIndex].HeaderText;
    }

    private void dgvUsers_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left // Obviously
            && e.RowIndex == -1 // column header row index
            && mouseDownColumnName == dgvUsers.Columns[e.ColumnIndex].HeaderText // No drag gesture
        ) {
            dgvUsers.Cursor = Cursors.WaitCursor;
            Task<DataTable> sortAction = Task<DataTable>.Factory.StartNew(() =>
            {
                DataView dvUsers = ((DataTable)dgvUsers.DataSource).DefaultView;
                string headerText = dgvUsers.Columns[e.ColumnIndex].HeaderText;
                if (sortExpression == 
quot;{headerText} asc")
                    dvUsers.Sort = 
quot;{headerText} desc";
                else
                    dvUsers.Sort = 
quot;{headerText} asc";
                sortExpression = dvUsers.Sort;
                return dvUsers.ToTable();
            });
            sortAction.Wait();
            dgvUsers.DataSource = sortAction.Result;

            dgvUsers.Cursor = Cursors.Default;
        }
    }

This appears to be faster than default "automatic" sorting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文