DataGridViewColumnCollection.GetColumnCount Method

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
Not applicable.



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 Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions