DataGridView.SortCompare 事件
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在 DataGridView 对两个单元格的值进行比较以执行排序操作时发生。
public:
event System::Windows::Forms::DataGridViewSortCompareEventHandler ^ SortCompare;
public event System.Windows.Forms.DataGridViewSortCompareEventHandler SortCompare;
public event System.Windows.Forms.DataGridViewSortCompareEventHandler? SortCompare;
member this.SortCompare : System.Windows.Forms.DataGridViewSortCompareEventHandler
Public Custom Event SortCompare As DataGridViewSortCompareEventHandler
事件类型
示例
下面的代码示例演示如何 SortCompare 在多列排序中使用 。 此示例是 How to: Customize Sorting in the Windows 窗体 DataGridView 控件中提供的更大示例的一部分。
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.CellValue1.ToString(), e.CellValue2.ToString());
// If the cells are equal, sort based on the ID column.
if (e.SortResult == 0 && e.Column.Name != "ID")
{
e.SortResult = System.String.Compare(
dataGridView1.Rows[e.RowIndex1].Cells["ID"].Value.ToString(),
dataGridView1.Rows[e.RowIndex2].Cells["ID"].Value.ToString());
}
e.Handled = true;
}
Private Sub DataGridView1_SortCompare( _
ByVal sender As Object, ByVal e As DataGridViewSortCompareEventArgs) _
Handles DataGridView1.SortCompare
' Try to sort based on the contents of the cell in the current column.
e.SortResult = System.String.Compare(e.CellValue1.ToString(), _
e.CellValue2.ToString())
' If the cells are equal, sort based on the ID column.
If (e.SortResult = 0) AndAlso Not (e.Column.Name = "ID") Then
e.SortResult = System.String.Compare( _
DataGridView1.Rows(e.RowIndex1).Cells("ID").Value.ToString(), _
DataGridView1.Rows(e.RowIndex2).Cells("ID").Value.ToString())
End If
e.Handled = True
End Sub
注解
仅当未设置 属性且VirtualMode属性值为 false
时DataSource,才会发生此事件。
此事件比较正在排序的列中的单元格对。 仅当用户单击属性值Automatic为 SortMode 的列的 标题时,或者调用Sort(DataGridViewColumn, ListSortDirection)重载时,才会发生此情况。 当属性值为 的Programmatic列SortMode发生此事件时,必须通过 DataGridViewColumnHeaderCell.SortGlyphDirection 属性自行显示排序字形。
可以使用此事件通过一列或多列中的单元格值对行进行排序。 CellValue1使用 和 CellValue2 属性比较 属性中指定的列中的Column单元格值。 RowIndex1使用 和 RowIndex2 属性通过 Rows 集合访问其他列中的值。
有关如何处理事件的详细信息,请参阅 处理和引发事件。