DataGridViewColumnCollection.GetColumnCount Method (DataGridViewElementStates)

 

Returns the number of columns that meet the given filter requirements.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public int GetColumnCount(
	DataGridViewElementStates includeFilter
)

Parameters

includeFilter
Type: System.Windows.Forms.DataGridViewElementStates

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

Return Value

Type: System.Int32

The number of columns that meet the filter requirements.

Exception Condition
ArgumentException

includeFilter is not a valid bitwise combination of DataGridViewElementStates values.

The following code example illustrates how to use this method to get the number of selected columns.

private void selectedColumnsButton_Click(object sender, System.EventArgs e)
{
    Int32 selectedColumnCount = dataGridView1.Columns
        .GetColumnCount(DataGridViewElementStates.Selected);
    if (selectedColumnCount > 0)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();

        for (int i = 0; i < selectedColumnCount; i++)
        {
            sb.Append("Column: ");
            sb.Append(dataGridView1.SelectedColumns[i].Index
                .ToString());
            sb.Append(Environment.NewLine);
        }

        sb.Append("Total: " + selectedColumnCount.ToString());
        MessageBox.Show(sb.ToString(), "Selected Columns");
    }
}

.NET Framework
Available since 2.0
Return to top
Show: