DataGridView.RowCount 속성

정의

DataGridView에 표시된 행의 수를 가져오거나 설정합니다.

public:
 property int RowCount { int get(); void set(int value); };
[System.ComponentModel.Browsable(false)]
public int RowCount { get; set; }
[<System.ComponentModel.Browsable(false)>]
member this.RowCount : int with get, set
Public Property RowCount As Integer

속성 값

DataGridView에 표시할 행의 수입니다.

특성

예외

이 속성을 설정할 지정된 값이 0보다 작습니다.

또는

지정된 값이 1보다 작고 AllowUserToAddRowstrue로 설정된 경우

이 속성을 설정할 때 DataSource 속성이 설정된 경우

예제

다음 코드 예제에서는 RowCount 속성입니다. 이 예제에서 이 속성은 의 항목 DataGridView수를 추적하는 데 사용됩니다. 이 예제는에서 사용할 수 있는 보다 큰 예제의 일부는 SelectionChanged 이벤트입니다.

private void UpdateLabelText()
{
    int WithdrawalTotal = 0;
    int DepositTotal = 0;
    int SelectedCellTotal = 0;
    int counter;

    // Iterate through all the rows and sum up the appropriate columns.
    for (counter = 0; counter < (DataGridView1.Rows.Count);
        counter++)
    {
        if (DataGridView1.Rows[counter].Cells["Withdrawals"].Value
            != null)
        {
            if (DataGridView1.Rows[counter].
                Cells["Withdrawals"].Value.ToString().Length != 0)
            {
                WithdrawalTotal += int.Parse(DataGridView1.Rows[counter].
                    Cells["Withdrawals"].Value.ToString());
            }
        }

        if (DataGridView1.Rows[counter].Cells["Deposits"].Value != null)
        {
            if (DataGridView1.Rows[counter]
                .Cells["Deposits"].Value.ToString().Length != 0)
            {
                DepositTotal += int.Parse(DataGridView1.Rows[counter]
                    .Cells["Deposits"].Value.ToString());
            }
        }
    }

    // Iterate through the SelectedCells collection and sum up the values.
    for (counter = 0;
        counter < (DataGridView1.SelectedCells.Count); counter++)
    {
        if (DataGridView1.SelectedCells[counter].FormattedValueType ==
            Type.GetType("System.String"))
        {
            string value = null;

            // If the cell contains a value that has not been commited,
            // use the modified value.
            if (DataGridView1.IsCurrentCellDirty == true)
            {

                value = DataGridView1.SelectedCells[counter]
                    .EditedFormattedValue.ToString();
            }
            else
            {
                value = DataGridView1.SelectedCells[counter]
                    .FormattedValue.ToString();
            }
            if (value != null)
            {
                // Ignore cells in the Description column.
                if (DataGridView1.SelectedCells[counter].ColumnIndex !=
                    DataGridView1.Columns["Description"].Index)
                {
                    if (value.Length != 0)
                    {
                        SelectedCellTotal += int.Parse(value);
                    }
                }
            }
        }
    }

    // Set the labels to reflect the current state of the DataGridView.
    Label1.Text = "Withdrawals Total: " + WithdrawalTotal.ToString();
    Label2.Text = "Deposits Total: " + DepositTotal.ToString();
    Label3.Text = "Selected Cells Total: " + SelectedCellTotal.ToString();
    Label4.Text = "Total entries: " + DataGridView1.RowCount.ToString();
}
Private Sub UpdateLabelText()
    Dim WithdrawalTotal As Integer = 0
    Dim DepositTotal As Integer = 0
    Dim SelectedCellTotal As Integer = 0
    Dim counter As Integer

    ' Iterate through all the rows and sum up the appropriate columns.
    For counter = 0 To (DataGridView1.Rows.Count - 1)
        If Not DataGridView1.Rows(counter) _
            .Cells("Withdrawals").Value Is Nothing Then

            If Not DataGridView1.Rows(counter) _
                .Cells("Withdrawals").Value.ToString().Length = 0 Then

                WithdrawalTotal += _
                    Integer.Parse(DataGridView1.Rows(counter) _
                    .Cells("Withdrawals").Value.ToString())
            End If
        End If

        If Not DataGridView1.Rows(counter) _
            .Cells("Deposits").Value Is Nothing Then

            If Not DataGridView1.Rows(counter) _
                .Cells("Deposits").Value.ToString().Length = 0 Then

                DepositTotal += _
                    Integer.Parse(DataGridView1.Rows(counter) _
                    .Cells("Deposits").Value.ToString())
            End If
        End If
    Next

    ' Iterate through the SelectedCells collection and sum up the values.
    For counter = 0 To (DataGridView1.SelectedCells.Count - 1)
        If DataGridView1.SelectedCells(counter).FormattedValueType Is _
        Type.GetType("System.String") Then

            Dim value As String = Nothing

            ' If the cell contains a value that has not been commited,
            ' use the modified value.
            If (DataGridView1.IsCurrentCellDirty = True) Then

                value = DataGridView1.SelectedCells(counter) _
                    .EditedFormattedValue.ToString()
            Else

                value = DataGridView1.SelectedCells(counter) _
                    .FormattedValue.ToString()
            End If

            If value IsNot Nothing Then

                ' Ignore cells in the Description column.
                If Not DataGridView1.SelectedCells(counter).ColumnIndex = _
                    DataGridView1.Columns("Description").Index Then

                    If Not value.Length = 0 Then
                        SelectedCellTotal += Integer.Parse(value)
                    End If
                End If
            End If
        End If

    Next

    ' Set the labels to reflect the current state of the DataGridView.
    Label1.Text = "Withdrawals Total: " & WithdrawalTotal.ToString()
    Label2.Text = "Deposits Total: " & DepositTotal.ToString()
    Label3.Text = "Selected Cells Total: " & SelectedCellTotal.ToString()
    Label4.Text = "Total entries: " & DataGridView1.RowCount.ToString()
End Sub

설명

가 현재 값보다 작은 값으로 설정된 경우 RowCount 컬렉션의 Rows 끝에서 행이 제거됩니다. 가 현재 값보다 큰 값으로 설정된 경우 RowCount 행이 컬렉션의 Rows 끝에 추가됩니다. 추가 행은 속성에 지정된 행을 RowTemplate 기반으로 합니다.

속성을 0으로 설정 RowCount 하면 모든 행이 에서 DataGridView제거됩니다. 이는 메서드를 호출하는 DataGridViewRowCollection.Clear 것과 같습니다.

참고

이 이trueAllowUserToAddRows 0으로 설정할 RowCount 수 없습니다. 이 경우 메서드를 DataGridViewRowCollection.Clear 호출하여 새 레코드의 행을 제외한 모든 행을 제거합니다. 이 경우 호출 Clear 의 결과는 1로 설정 RowCount 하지만 훨씬 빠릅니다.

RowCount 속성을 사용 하 고 ColumnCount 텍스트를 표시 하 고 편집 하는 간단한 DataGridView 만들려면 속성입니다. 속성을 열이 없는 컨트롤에 대해 0보다 큰 값으로 DataGridView 설정 RowCount 하면 가 DataGridViewTextBoxColumn 자동으로 추가됩니다.

적용 대상

추가 정보