DataGridViewColumnCollection.GetColumnCount Method

Note: This method is new in the .NET Framework version 2.0.

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

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

public int GetColumnCount (
	DataGridViewElementStates includeFilter
public int GetColumnCount (
	DataGridViewElementStates includeFilter
public function GetColumnCount (
	includeFilter : DataGridViewElementStates
) : int



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

Return Value

The number of columns that meet the filter requirements.

Exception typeCondition


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
    if (selectedColumnCount > 0)
        System.Text.StringBuilder sb = new System.Text.StringBuilder();

        for (int i = 0; i < selectedColumnCount; i++)
            sb.Append("Column: ");

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

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0