[英]How can I sort a WinForms DataGridView on a CheckBox column?
So I had a DataGridView
with autogenerated columns, some of which were checkbox columns. 所以我有一个带有自动生成列的
DataGridView
,其中一些是复选框列。 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?
我对其进行了研究,结果发现Microsoft不包括复选框列的自动排序...我认为这很荒谬-选中/不选中的排序有多难?
How can you get a DataGridView
to sort check box columns for you? 您如何获得一个
DataGridView
来为您排序复选框列?
Here's what I came up with: 这是我想出的:
You could also simply do this: 您也可以简单地这样做:
DataGridView.Columns("ColumnOfChoice").SortMode = DataGridViewColumnSortMode.Automatic
Works in vb.net 4.0 在vb.net 4.0中工作
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
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?
微软,现在真的那么难吗? ;-)
;-)
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. 似乎大多数列的默认值为“自动”,但复选框(布尔)列的默认值为“ NotSortable”。
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 ; 如果要将DataGridView绑定到一个简单的List it WONT WORK,则需要使用其他东西,我建议您使用此SortableBindingList ; You can pass directly your original List IEnumerable to the SortableBindingList's constructor.
您可以直接将原始List 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 :) 然后魔术就会发生:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.