DataGridViewColumnCollection.GetLastColumn Method

Definition

Returns the last column in display order that meets the given filter requirements.

public System.Windows.Forms.DataGridViewColumn GetLastColumn (System.Windows.Forms.DataGridViewElementStates includeFilter, System.Windows.Forms.DataGridViewElementStates excludeFilter);
public System.Windows.Forms.DataGridViewColumn? GetLastColumn (System.Windows.Forms.DataGridViewElementStates includeFilter, System.Windows.Forms.DataGridViewElementStates excludeFilter);

Parameters

includeFilter
DataGridViewElementStates

A bitwise combination of the DataGridViewElementStates values that represent the filter to apply for inclusion.

excludeFilter
DataGridViewElementStates

A bitwise combination of the DataGridViewElementStates values that represent the filter to apply for exclusion.

Returns

The last displayed column in display order that meets the given filter requirements, or null if no column is found.

Exceptions

At least one of the filter values is not a valid bitwise combination of DataGridViewElementStates values.

Examples

The following code example uses the GetLastColumn method to swap the last displayed column and the first displayed column.

// Swap the last column with the first.
private void Button10_Click(object sender, EventArgs args)
{
    DataGridViewColumnCollection columnCollection = dataGridView.Columns;

    DataGridViewColumn firstVisibleColumn =
        columnCollection.GetFirstColumn(DataGridViewElementStates.Visible);
    DataGridViewColumn lastVisibleColumn =
        columnCollection.GetLastColumn(
            DataGridViewElementStates.Visible, DataGridViewElementStates.None);

    int firstColumn_sIndex = firstVisibleColumn.DisplayIndex;
    firstVisibleColumn.DisplayIndex = lastVisibleColumn.DisplayIndex;
    lastVisibleColumn.DisplayIndex = firstColumn_sIndex;
}

Remarks

The last column in display order is the column with the highest DisplayIndex value, regardless of whether the column is actually visible on the screen.

This method lets you determine the last column that fits the given criteria without having to compare index values directly.

Applies to

Produit Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

See also