DataGridViewSortCompareEventArgs.SortResult 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定值,表示比較的儲存格將要排序的次序。
public:
property int SortResult { int get(); void set(int value); };
public int SortResult { get; set; }
member this.SortResult : int with get, set
Public Property SortResult As Integer
屬性值
如果第一個儲存格將排序在第二個儲存格之前,則會小於零;如果第一個儲存格和第二個儲存格有相同的值,則為零;如果第二個儲存格將排序在第一個儲存格之前,則為大於零。
範例
下列程式碼範例示範如何在 SortResult 多個資料行排序中使用 。 此範例是How to: Customize Sorting in the Windows Forms DataGridView Control中所提供的較大範例的一部分。
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
備註
設定此值通常是您在 事件的處理常式 DataGridView.SortCompare 中執行的最後一個作業。 您通常會將此值設定為比較方法的傳回值,例如 String.Compare 。
適用於
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應