vb.net 如何在 CheckBox 列上对 WinForms DataGridView 进行排序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14735260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:14:08  来源:igfitidea点击:

How can I sort a WinForms DataGridView on a CheckBox column?

vb.netdatagridviewdatagridviewcheckboxcell

提问by John

So I had a DataGridViewwith autogenerated columns, some of which were checkbox columns. When I clicked on the check box column's header, it didn't sort. I researched it and it turns out that Microsoft didn't include automatic sorting for checkbox columns... Which I think is absurd--how hard is it to sort checked / not checked?

所以我有一个DataGridView自动生成的列,其中一些是复选框列。当我单击复选框列的标题时,它没有排序。我研究了它,结果发现 Microsoft 没有包括复选框列的自动排序......我认为这是荒谬的 - 对选中/不选中进行排序有多难?

How can you get a DataGridViewto sort check box columns for you?

你怎么能得到一个DataGridView对你的复选框列进行排序?

Here's what I came up with:

这是我想出的:

回答by Jendrik

You could also simply do this:

你也可以简单地这样做:

DataGridView.Columns("ColumnOfChoice").SortMode = DataGridViewColumnSortMode.Automatic

Works in vb.net 4.0

在 vb.net 4.0 中工作

回答by Miguel Peinado

You only need to add next lines to the code of the form (tested in VB.NET 2013)

您只需要在表单的代码中添加下一行(在 VB.NET 2013 中测试)

Private Sub DataGridView1_ColumnAdded(sender As Object, e As System.Windows.Forms.DataGridViewColumnEventArgs) Handles DataGridView1.ColumnAdded
    If e.Column.GetType Is GetType(DataGridViewCheckBoxColumn) Then
        e.Column.SortMode = DataGridViewColumnSortMode.Automatic
    End If
End Sub

回答by John

First you need to hook into two events, the column added event and the column header click event:

首先需要钩入两个事件,列添加事件和列标题点击事件:

AddHandler dg.ColumnAdded, AddressOf dgColumnAdded
AddHandler dg.ColumnHeaderMouseClick, AddressOf dgSortColumns

Then, enable programmatic sorting for each check box column:

然后,为每个复选框列启用编程排序:

Private Sub dgColumnAdded(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DataGridViewColumnEventArgs)
    If e.Column.GetType Is GetType(DataGridViewCheckBoxColumn) Then
        e.Column.SortMode = DataGridViewColumnSortMode.Programmatic
    End If
End Sub

Then, create a handler that will sort a checkbox column, but do nothing for columns that will handle their own sorting:

然后,创建一个对复选框列进行排序的处理程序,但对将处理自己的排序的列不做任何事情:

Private Sub dgSortColumns(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs)
    Dim dg As DataGridView = sender
    Dim c As DataGridViewColumn = dg.Columns(e.ColumnIndex)
    If c.SortMode = DataGridViewColumnSortMode.Programmatic Then
        If dg.SortedColumn IsNot Nothing _
        AndAlso dg.SortedColumn.Name <> c.Name Then
            dg.Sort(c, System.ComponentModel.ListSortDirection.Ascending)
        Else
            Select Case dg.SortOrder
                Case Windows.Forms.SortOrder.None
                    dg.Sort(c, System.ComponentModel.ListSortDirection.Ascending)
                Case Windows.Forms.SortOrder.Ascending
                    dg.Sort(c, System.ComponentModel.ListSortDirection.Descending)
                Case Windows.Forms.SortOrder.Descending
                    dg.Sort(c, System.ComponentModel.ListSortDirection.Ascending)
            End Select
        End If
    End If
End Sub

And there you go! Now was it really that hard, Microsoft? ;-)

你去吧!现在真的有那么难吗,微软?;-)

回答by David Reich

I'm not sure about VB, but for C# in VS2012 in the designer you can also set the SortMode.

我不确定 VB,但对于设计器中 VS2012 中的 C#,您也可以设置 SortMode。

Right-click on the DataGridView and go to "Edit Columns".

右键单击 DataGridView 并转到“编辑列”。

There's a drop-down for SortMode with a choice of NotSortable, Automatic, and Programmatic.

SortMode 有一个下拉菜单,可选择 NotSortable、Automatic 和 Programmatic。

It appears that the default for most columns is Automatic, but for checkboxes (boolean) columns the default is NotSortable.

大多数列的默认值似乎是 Automatic,但对于复选框(布尔值)列,默认值是 NotSortable。

回答by coloboxp

I have created an extension method that you can reuse, you just need to use it during the form load event.

我创建了一个可以重用的扩展方法,您只需要在表单加载事件期间使用它。

------ Be sure that your DataSource is sortable. ------

------ 确保您的数据源是可排序的。------

If you are binding the DataGridView to a simple List it WONT WORK, you need to use something else, I recommend you to use this SortableBindingList; You can pass directly your original List IEnumerable to the SortableBindingList's constructor.

如果你将 DataGridView 绑定到一个简单的 List 它不会工作,你需要使用其他东西,我建议你使用这个 SortableBindingList;您可以将原始列表 IEnumerable 直接传递给 SortableBindingList 的构造函数。

Load:

加载:

private void frmWithTheDataGrid_Load(object sender, EventArgs e)
{
    this.yourDataGridView.SortabilizeMe();
}

Then add this into a static class to use it as an ExtensionMethod..

然后将此添加到静态类中以将其用作 ExtensionMethod..

public static void SortabilizeMe(this DataGridView dgv)
{
    dgv.ColumnAdded+= delegate(object sender, DataGridViewColumnEventArgs args)
    {
        args.Column.SortMode = DataGridViewColumnSortMode.Programmatic;
    };

    dgv.ColumnHeaderMouseClick += delegate(object sender, DataGridViewCellMouseEventArgs args)
    {
        var col = dgv.Columns[args.ColumnIndex];

        if (dgv.SortedColumn != null && dgv.SortedColumn.Name != col.Name)
        {
            dgv.Sort(row, ListSortDirection.Ascending);
        }
        else
        {
            switch (dgv.SortOrder)
            {
                case SortOrder.None:
                    dgv.Sort(col, ListSortDirection.Ascending);
                    break;
                case SortOrder.Ascending:
                    dgv.Sort(col, ListSortDirection.Descending);
                    break;
                case SortOrder.Descending:
                    dgv.Sort(col, ListSortDirection.Ascending);
                    break;
            }
        }
    };
}

Then magic will happen :)

然后魔法会发生:)