DataGridViewColumnCollection.GetFirstColumn Method

Definition

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

Overloads

GetFirstColumn(DataGridViewElementStates)

Returns the first column in display order that meets the given inclusion-filter requirements.

GetFirstColumn(DataGridViewElementStates, DataGridViewElementStates)

Returns the first column in display order that meets the given inclusion-filter and exclusion-filter requirements.

GetFirstColumn(DataGridViewElementStates)

Source:
DataGridViewColumnCollection.cs
Source:
DataGridViewColumnCollection.cs
Source:
DataGridViewColumnCollection.cs

Returns the first column in display order that meets the given inclusion-filter requirements.

C#
public System.Windows.Forms.DataGridViewColumn GetFirstColumn(System.Windows.Forms.DataGridViewElementStates includeFilter);
C#
public System.Windows.Forms.DataGridViewColumn? GetFirstColumn(System.Windows.Forms.DataGridViewElementStates includeFilter);

Parameters

includeFilter
DataGridViewElementStates

A bitwise combination of the DataGridViewElementStates values that represents the filter for inclusion.

Returns

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

Exceptions

includeFilter is not a valid bitwise combination of DataGridViewElementStates values.

Examples

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

C#
// 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 first column in display order is the column with the lowest DisplayIndex value, regardless of whether the column is actually visible on the screen.

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

See also

Applies to

.NET Framework 4.8.1 and other versions
Product 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, 10

GetFirstColumn(DataGridViewElementStates, DataGridViewElementStates)

Source:
DataGridViewColumnCollection.cs
Source:
DataGridViewColumnCollection.cs
Source:
DataGridViewColumnCollection.cs

Returns the first column in display order that meets the given inclusion-filter and exclusion-filter requirements.

C#
public System.Windows.Forms.DataGridViewColumn GetFirstColumn(System.Windows.Forms.DataGridViewElementStates includeFilter, System.Windows.Forms.DataGridViewElementStates excludeFilter);
C#
public System.Windows.Forms.DataGridViewColumn? GetFirstColumn(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 first 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.

Remarks

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

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

See also

Applies to

.NET Framework 4.8.1 and other versions
Product 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, 10