设置 datagridview 的最后一行不可排序(始终位于底部)

发布于 2024-09-17 11:15:55 字数 711 浏览 2 评论 0原文

我有一个可排序的 DatagridView,其摘要最后一行包含一些列的总和,我想将此摘要行始终保留为 datagridView 的最后一行(底部)。

目前,当我对 datagridView 的列进行排序时,摘要行也会进行排序,我不希望这样。我想保留 DatagridView 的最后一行(摘要行)不可排序。

有办法做到吗?

我找到了解决方案:

我以这种方式重载了 sortcompare 方法:

  private void grid_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
    {
        try
        {
            if (e.RowIndex1 == this.dataGridView1.Rows.Count -1)
                e.Handled = true;
            if (e.RowIndex2 == this.dataGridView1.Rows.Count - 1)
                e.Handled = true;
            return;
        }
        catch (Exception ex)
        {
            ex.ToString();
        }
    }

I have a sortable DatagridView with a summary last row containing sum of some columns, i want to keep this summary row always as the last line(bottom) of datagridView.

At the moment, when i sort a column of datagridView also the summary row get sorted, i don't want this. I want to keep the last row (summary row) of DatagridView not sortable .

Is there a way to do it ?

I've found a solution :

I've overloaded the sortcompare method in this manner :

  private void grid_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
    {
        try
        {
            if (e.RowIndex1 == this.dataGridView1.Rows.Count -1)
                e.Handled = true;
            if (e.RowIndex2 == this.dataGridView1.Rows.Count - 1)
                e.Handled = true;
            return;
        }
        catch (Exception ex)
        {
            ex.ToString();
        }
    }

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

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

发布评论

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

评论(1

最终幸福 2024-09-24 11:15:56

您可以向数据表添加一列,并将所有非汇总记录的值设置为 0。将汇总记录值设置为 1,然后对“汇总”列进行排序,然后对其他列进行排序。

这在内存中可能表现不佳,因此如果可能的话,您希望在 SQL 查询中执行此操作,具体取决于您使用的数据库。

You could add a column to the datatable, and set the value for all non-summary records to 0. Set the summary record value to 1, then sort on the "summary" column, then others.

This might not perform well in memory, so you'd want to do it in the SQL query if possible, depending on the database you use.

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