DataGridViewColumn初始排序方向

发布于 2024-07-29 21:32:41 字数 188 浏览 6 评论 0原文

我正在 VS2008 上开发 C# WinForms 应用程序。 默认情况下,当单击 DataGridView 中的列标题时,它会按升序对该列进行排序,然后您可以再次单击列标题以降序对其进行排序。

我试图扭转这一点,因此初始单击按降序排序,然后第二次单击按升序排序,我无法弄清楚如何执行此操作。 有人知道吗?

谢谢

I'm working in VS2008 on a C# WinForms app. By default when clicking on a column header in a DataGridView it sorts that column Ascending, you can then click on the column header again to sort it Descending.

I am trying to reverse this, so the initial click sorts Descending then the second click sorts Ascending and I haven't been able to figure out how to do this. Does anyone know?

Thanks

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

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

发布评论

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

评论(4

贩梦商人 2024-08-05 21:32:41

您可以将 HeaderCell SortGlyphDirection 设置为 Ascending,然后下次单击将为您提供降序排列。 默认为无。

dataGridView1.Sort(Column1, ListSortDirection.Ascending);
this.Column1.HeaderCell.SortGlyphDirection = System.Windows.Forms.SortOrder.Ascending;

You can set the HeaderCell SortGlyphDirection to Ascending, and then the next click will give you the descending order. The default is none.

dataGridView1.Sort(Column1, ListSortDirection.Ascending);
this.Column1.HeaderCell.SortGlyphDirection = System.Windows.Forms.SortOrder.Ascending;
黑白记忆 2024-08-05 21:32:41
foreach (DataGridViewColumn column in DataGridView1.Columns)
{
    column.SortMode = DataGridViewColumnSortMode.Programmatic;
}

private void DataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
    var column = DataGridView1.Columns[e.ColumnIndex];
    if (column.SortMode != DataGridViewColumnSortMode.Programmatic)
        return;

    var sortGlyph = column.HeaderCell.SortGlyphDirection;
    switch (sortGlyph)
    {
        case SortOrder.None:
        case SortOrder.Ascending:
            DataGridView1.Sort(column, ListSortDirection.Descending);
            column.HeaderCell.SortGlyphDirection = SortOrder.Descending;
            break;
        case SortOrder.Descending:
            DataGridView1.Sort(column, ListSortDirection.Ascending);
            column.HeaderCell.SortGlyphDirection = SortOrder.Ascending;
            break;
    }
}
foreach (DataGridViewColumn column in DataGridView1.Columns)
{
    column.SortMode = DataGridViewColumnSortMode.Programmatic;
}

and

private void DataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
    var column = DataGridView1.Columns[e.ColumnIndex];
    if (column.SortMode != DataGridViewColumnSortMode.Programmatic)
        return;

    var sortGlyph = column.HeaderCell.SortGlyphDirection;
    switch (sortGlyph)
    {
        case SortOrder.None:
        case SortOrder.Ascending:
            DataGridView1.Sort(column, ListSortDirection.Descending);
            column.HeaderCell.SortGlyphDirection = SortOrder.Descending;
            break;
        case SortOrder.Descending:
            DataGridView1.Sort(column, ListSortDirection.Ascending);
            column.HeaderCell.SortGlyphDirection = SortOrder.Ascending;
            break;
    }
}
七度光 2024-08-05 21:32:41

我建议下面的代码

MyDGV.Sort(MyDGV.Columns[column_Index], ListSortDirection.Ascending);

I suggest below code

MyDGV.Sort(MyDGV.Columns[column_Index], ListSortDirection.Ascending);
少女净妖师 2024-08-05 21:32:41

看一下 DataGridView.SortCompare
请参阅下面的 msdn 示例的稍微修改版本:

private void dataGridView1_SortCompare(object sender,
        DataGridViewSortCompareEventArgs e)
    {
        // Try to sort based on the cells in the current column.
        e.SortResult = System.String.Compare(
            e.CellValue2.ToString(), e.CellValue1.ToString()); // descending sort

        e.Handled = true;
    }

Take a look at DataGridView.SortCompare.
See slightly modified version of the msdn example below:

private void dataGridView1_SortCompare(object sender,
        DataGridViewSortCompareEventArgs e)
    {
        // Try to sort based on the cells in the current column.
        e.SortResult = System.String.Compare(
            e.CellValue2.ToString(), e.CellValue1.ToString()); // descending sort

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