DataGridViewRow.Cells 属性

定义

获取用于填充行的单元格集合。

public:
 property System::Windows::Forms::DataGridViewCellCollection ^ Cells { System::Windows::Forms::DataGridViewCellCollection ^ get(); };
[System.ComponentModel.Browsable(false)]
public System.Windows.Forms.DataGridViewCellCollection Cells { get; }
[<System.ComponentModel.Browsable(false)>]
member this.Cells : System.Windows.Forms.DataGridViewCellCollection
Public ReadOnly Property Cells As DataGridViewCellCollection

属性值

DataGridViewCellCollection

一个包含行中的所有单元格的 DataGridViewCellCollection

属性

示例

下面的代码示例使用该 Cells 属性设置行中单元格的值。 此代码示例是 How to: 操作 Windows 窗体 DataGridView 控件中的行的大型代码示例的一部分。

// Give cheescake excellent rating.
void Button8_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
   UpdateStars( dataGridView->Rows[ 4 ], L"******************" );
}

int ratingColumn;
void UpdateStars( DataGridViewRow^ row, String^ stars )
{
   row->Cells[ ratingColumn ]->Value = stars;
   
   // Resize the column width to account for the new value.
   row->DataGridView->AutoResizeColumn( ratingColumn, DataGridViewAutoSizeColumnMode::DisplayedCells );
}
// Give cheescake excellent rating.
private void Button8_Click(object sender,
    System.EventArgs e)
{
    UpdateStars(dataGridView.Rows[4], "******************");
}

int ratingColumn = 3;

private void UpdateStars(DataGridViewRow row, string stars)
{

    row.Cells[ratingColumn].Value = stars;

    // Resize the column width to account for the new value.
    row.DataGridView.AutoResizeColumn(ratingColumn, 
        DataGridViewAutoSizeColumnMode.DisplayedCells);
}
' Give cheescake excellent rating.
Private Sub Button8_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles Button8.Click

    UpdateStars(dataGridView.Rows(4), "******************")
End Sub

Private ratingColumn As Integer = 3

Private Sub UpdateStars(ByVal row As DataGridViewRow, _
    ByVal stars As String)

    row.Cells(ratingColumn).Value = stars

    ' Resize the column width to account for the new value.
    row.DataGridView.AutoResizeColumn(ratingColumn, _
        DataGridViewAutoSizeColumnMode.DisplayedCells)

End Sub

注解

如果访问此属性时该行不包含任何单元格,则调用CreateCellsInstance该方法将创建一个新的空DataGridViewCellCollection

适用于

另请参阅