DataGridView.GetCellCount(DataGridViewElementStates) 方法

定义

获取满足所提供筛选器的单元格的数目。Gets the number of cells that satisfy the provided filter.

public:
 int GetCellCount(System::Windows::Forms::DataGridViewElementStates includeFilter);
public int GetCellCount (System.Windows.Forms.DataGridViewElementStates includeFilter);
member this.GetCellCount : System.Windows.Forms.DataGridViewElementStates -> int
Public Function GetCellCount (includeFilter As DataGridViewElementStates) As Integer

参数

includeFilter
DataGridViewElementStates

DataGridViewElementStates 值的按位组合,指定要计数的单元格。A bitwise combination of the DataGridViewElementStates values specifying the cells to count.

返回

includeFilter 参数匹配的单元格的数目。The number of cells that match the includeFilter parameter.

异常

includeFilter 包括 ResizableSet 值。includeFilter includes the value ResizableSet.

示例

下面的代码示例演示如何使用此方法来确定 @no__t 0 控件中是否有任何单元格处于选定状态。The following code example illustrates how to use this method to determine whether there are any cells selected in a DataGridView control. 在此示例中,如果选择了任何单元,则会通过 GetClipboardContent 方法检索它们的值并将其显示在 @no__t 1 控件中。In this example, if any cells are selected, their values are retrieved through the GetClipboardContent method and displayed in a TextBox control.

此代码是一个更大示例的一部分,用于说明如何使用 DataGridView 控件的剪贴板功能。This code is part of a larger example illustrating the use of the Clipboard features of the DataGridView control. 此示例摘自 [How 中提供的更大示例的一部分:使用户能够将多个单元格从 Windows 窗体 DataGridView 控件 @ no__t 复制到剪贴板。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 CopyPasteButton_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.";
        }
    }
}
Private Sub CopyPasteButton_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles CopyPasteButton.Click

    If Me.DataGridView1.GetCellCount( _
        DataGridViewElementStates.Selected) > 0 Then

        Try

            ' Add the selection to the clipboard.
            Clipboard.SetDataObject( _
                Me.DataGridView1.GetClipboardContent())

            ' Replace the text box contents with the clipboard text.
            Me.TextBox1.Text = Clipboard.GetText()

        Catch ex As System.Runtime.InteropServices.ExternalException
            Me.TextBox1.Text = _
                "The Clipboard could not be accessed. Please try again."
        End Try

    End If

End Sub

注解

此方法可用于确定处于特定状态的单元格的数目。This method is useful to determine the number of cells in a particular state. 例如,若要检索所选单元格的数目,请将此方法用于 @no__t 值。To retrieve the number of selected cells, for example, use this method with the DataGridViewElementStates.Selected value. 这通常比使用 SelectedCells 属性更有效。This is typically more efficient than using the SelectedCells property.

适用于

另请参阅