Export (0) Print
Expand All
Expand Minimize

DataGridView.GetCellCount Method

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

Gets the number of cells that satisfy the provided filter.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

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

Parameters

includeFilter

A bitwise combination of the DataGridViewElementStates values specifying the cells to count.

Return Value

The number of cells that match the includeFilter parameter.

Exception typeCondition

ArgumentException

includeFilter includes the value ResizableSet.

This method is useful to determine the number of cells in a particular state. To retrieve the number of selected cells, for example, use this method with the DataGridViewElementStates.Selected value. This is typically more efficient than using the SelectedCells property.

The following code example illustrates how to use this method to determine whether there are any cells selected in a DataGridView control. In this example, if any cells are selected, their values are retrieved through the GetClipboardContent method and displayed in a TextBox control.

This code is part of a larger example illustrating the use of the Clipboard features of the DataGridView control. This example is part of a larger example available in How to: Enable Users to Copy Multiple Cells to the Clipboard from the Windows Forms DataGridView Control.

private void PasteButton_Click(object sender, System.EventArgs e)
{
    if (this.DataGridView1
        .GetCellCount(DataGridViewElementStates.Selected) > 0)
    {
        try
        {
            // Add the selection to the clipboard.
            Clipboard.SetDataObject(
                this.DataGridView1.GetClipboardContent());
            
            // Replace the text box contents with the clipboard text.
            this.TextBox1.Text = Clipboard.GetText();
        }
        catch (System.Runtime.InteropServices.ExternalException)
        {
            this.TextBox1.Text = 
                "The Clipboard could not be accessed. Please try again.";
        }
    }
}

Windows 98, Windows 2000 SP4, Windows Millennium Edition, 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

Community Additions

ADD
Show:
© 2014 Microsoft