DataGridViewSortCompareEventArgs.SortResult Property
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets or sets a value indicating the order in which the compared cells will be sorted.
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
Property Value
Less than zero if the first cell will be sorted before the second cell; zero if the first cell and second cell have equivalent values; greater than zero if the second cell will be sorted before the first cell.
Examples
The following code example demonstrates the use of SortResult in a multiple column sort. This example is part of a larger example provided in 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
Remarks
Setting this value is typically the last operation you will perform in a handler for the DataGridView.SortCompare event. You will typically set this value to the return value of a comparison method such as String.Compare.