我尝试在GridView中进行排序。我接受代码here。并将其放入ColumnHeaderMouseClick
事件:
private void УчасткиDataGridViewColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
// Check which column is selected, otherwise set NewColumn to null.
var newColumn =
УчасткиDataGridView.Columns.GetColumnCount(
DataGridViewElementStates.Selected) == 1 ?
УчасткиDataGridView.SelectedColumns[0] : null;
MessageBox.Show(УчасткиDataGridView.SelectedColumns.Count.ToString());
var oldColumn = УчасткиDataGridView.SortedColumn;
ListSortDirection direction;
if (oldColumn != null)
{
// Sort the same column again, reversing the SortOrder.
if (oldColumn == newColumn &&
УчасткиDataGridView.SortOrder == SortOrder.Ascending)
{
direction = ListSortDirection.Descending;
}
else
{
// Sort a new column and remove the old SortGlyph.
direction = ListSortDirection.Ascending;
oldColumn.HeaderCell.SortGlyphDirection = SortOrder.None;
}
}
else
{
direction = ListSortDirection.Ascending;
}
if (newColumn == null)
{
MessageBox.Show("Select a single column and try again.",
"Error: Invalid Selection", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
else
{
УчасткиDataGridView.Sort(newColumn, direction);
newColumn.HeaderCell.SortGlyphDirection =
direction == ListSortDirection.Ascending ?
SortOrder.Ascending : SortOrder.Descending;
}
}
但是在УчасткиDataGridView.Sort(newColumn, direction);
行中,我得到了例外:
IBindingList does not support sorting
有什么不对?