Поделиться через


DataGridView.GetCellCount(DataGridViewElementStates) Метод

Определение

Возвращает количество ячеек, удовлетворяющих предоставленному фильтру.

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 , указывающих ячейки для подсчета.

Возвращаемое значение

Количество ячеек, соответствующих параметру includeFilter .

Исключения

includeFilter включает значение ResizableSet.

Примеры

В следующем примере кода показано, как использовать этот метод для определения наличия ячеек, выбранных в элементе DataGridView управления. В этом примере, если выбраны какие-либо ячейки, их значения извлекаются с помощью GetClipboardContent метода и отображаются в элементе TextBox управления.

Этот код является частью более крупного примера, демонстрирующего использование функций DataGridView буфера обмена элемента управления. Этот пример является частью более крупного примера, доступного в разделе "Практическое руководство. Включение пользователей для копирования нескольких ячеек в буфер обмена из элемента управления DataGridView в Windows Forms".

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

Комментарии

Этот метод полезен для определения количества ячеек в определенном состоянии. Чтобы получить количество выбранных ячеек, например, используйте этот метод со значением DataGridViewElementStates.Selected . Обычно это более эффективно, чем использование SelectedCells свойства.

Применяется к

См. также раздел