0

そのためDataGridView、自動生成された列があり、その一部はチェックボックス列でした。チェックボックスの列のヘッダーをクリックしても、並べ替えられませんでした。調べてみたところ、Microsoftにはチェックボックス列の自動並べ替えが含まれていなかったことがわかりました...これはばかげていると思います。チェックされている/チェックされていない並べ替えはどれくらい難しいですか?

チェックボックスの列を並べ替えるにはどうすればよいDataGridViewですか?

これが私が思いついたものです:

4

5 に答える 5

2

You could also simply do this:

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

Works in vb.net 4.0

于 2013-11-13T10:06:15.360 に答える
2

You only need to add next lines to the code of the form (tested in 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
于 2015-04-24T14:07:15.837 に答える
1

まず、列追加イベントと列ヘッダークリックイベントの2つのイベントにフックする必要があります。

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

次に、チェックボックスの列ごとにプログラムによる並べ替えを有効にします。

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

次に、チェックボックス列を並べ替えるハンドラーを作成しますが、独自の並べ替えを処理する列には何もしません。

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

そして、あなたは行き​​ます!マイクロソフト、本当に大変でしたか?;-)

于 2013-02-06T17:33:44.467 に答える
0

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

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

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

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

于 2013-10-20T03:30:48.447 に答える
0

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.

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..

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 :)

于 2014-07-24T14:16:43.417 に答える