DataGridViewColumnHeaderCell.SortGlyphDirection DataGridViewColumnHeaderCell.SortGlyphDirection DataGridViewColumnHeaderCell.SortGlyphDirection DataGridViewColumnHeaderCell.SortGlyphDirection Property

定義

表示されている並べ替えグリフを示す値を取得または設定します。Gets or sets a value indicating which sort glyph is displayed.

public:
 property System::Windows::Forms::SortOrder SortGlyphDirection { System::Windows::Forms::SortOrder get(); void set(System::Windows::Forms::SortOrder value); };
public System.Windows.Forms.SortOrder SortGlyphDirection { get; set; }
member this.SortGlyphDirection : System.Windows.Forms.SortOrder with get, set
Public Property SortGlyphDirection As SortOrder

プロパティ値

現在のグリフを表す SortOrder 値。A SortOrder value representing the current glyph. 既定値は、None です。The default is None.

例外

このプロパティの設定時に指定した値が、有効な SortOrder 値ではありません。The specified value when setting this property is not a valid SortOrder value.

このプロパティの設定時に、セルの OwningColumn プロパティまたは DataGridView プロパティのいずれかの値が null です。When setting this property, the value of either the OwningColumn property or the DataGridView property of the cell is null.

- または --or- このプロパティの値の変更時に、指定された値が None ではなく、所有している列の SortMode プロパティの値が NotSortable です。When changing the value of this property, the specified value is not None and the value of the SortMode property of the owning column is NotSortable.

次のコードは、プログラムによるSortGlyphDirection並べ替えでプロパティを使用する方法を示しています。The following code demonstrates how to use the SortGlyphDirection property in a programmatic sort.

private void sortButton_Click(object sender, System.EventArgs e)
{
    // Check which column is selected, otherwise set NewColumn to null.
    DataGridViewColumn newColumn =
        dataGridView1.Columns.GetColumnCount(
        DataGridViewElementStates.Selected) == 1 ?
        dataGridView1.SelectedColumns[0] : null;

    DataGridViewColumn oldColumn = dataGridView1.SortedColumn;
    ListSortDirection direction;

    // If oldColumn is null, then the DataGridView is not currently sorted.
    if (oldColumn != null)
    {
        // Sort the same column again, reversing the SortOrder.
        if (oldColumn == newColumn &&
            dataGridView1.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 no column has been selected, display an error dialog  box.
    if (newColumn == null)
    {
        MessageBox.Show("Select a single column and try again.",
            "Error: Invalid Selection", MessageBoxButtons.OK,
            MessageBoxIcon.Error);
    }
    else
    {
        dataGridView1.Sort(newColumn, direction);
        newColumn.HeaderCell.SortGlyphDirection =
            direction == ListSortDirection.Ascending ?
            SortOrder.Ascending : SortOrder.Descending;
    }
}
Private Sub SortButton_Click(ByVal sender As Object, _
    ByVal e As EventArgs) Handles sortButton.Click

    ' Check which column is selected, otherwise set NewColumn to Nothing.
    Dim newColumn As DataGridViewColumn
    If dataGridView1.Columns.GetColumnCount(DataGridViewElementStates _
        .Selected) = 1 Then
        newColumn = dataGridView1.SelectedColumns(0)
    Else
        newColumn = Nothing
    End If

    Dim oldColumn As DataGridViewColumn = dataGridView1.SortedColumn
    Dim direction As ListSortDirection

    ' If oldColumn is null, then the DataGridView is not currently sorted.
    If oldColumn IsNot Nothing Then

        ' Sort the same column again, reversing the SortOrder.
        If oldColumn Is newColumn AndAlso dataGridView1.SortOrder = _
            SortOrder.Ascending Then
            direction = ListSortDirection.Descending
        Else

            ' Sort a new column and remove the old SortGlyph.
            direction = ListSortDirection.Ascending
            oldColumn.HeaderCell.SortGlyphDirection = SortOrder.None
        End If
    Else
        direction = ListSortDirection.Ascending
    End If


    ' If no column has been selected, display an error dialog  box.
    If newColumn Is Nothing Then
        MessageBox.Show("Select a single column and try again.", _
            "Error: Invalid Selection", MessageBoxButtons.OK, _
            MessageBoxIcon.Error)
    Else
        dataGridView1.Sort(newColumn, direction)
        If direction = ListSortDirection.Ascending Then
            newColumn.HeaderCell.SortGlyphDirection = SortOrder.Ascending
        Else
            newColumn.HeaderCell.SortGlyphDirection = SortOrder.Descending
        End If
    End If

End Sub

注釈

このプロパティは、 SortModeプロパティがにDataGridViewColumnSortMode.Automatic設定されている列に対して自動的に設定されます。This property is set automatically for columns with the SortMode property set to DataGridViewColumnSortMode.Automatic. プロパティがにSortMode DataGridViewColumnSortMode.Programmatic設定された列で並べ替えを行う場合は、このプロパティを自分で設定する必要があります。You must set this property yourself when sorting by columns with the SortMode property set to DataGridViewColumnSortMode.Programmatic.

適用対象

こちらもご覧ください