DataGridView.SelectedCells 属性    
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取用户选定的单元格的集合。
public:
 property System::Windows::Forms::DataGridViewSelectedCellCollection ^ SelectedCells { System::Windows::Forms::DataGridViewSelectedCellCollection ^ get(); };[System.ComponentModel.Browsable(false)]
public System.Windows.Forms.DataGridViewSelectedCellCollection SelectedCells { get; }[<System.ComponentModel.Browsable(false)>]
member this.SelectedCells : System.Windows.Forms.DataGridViewSelectedCellCollectionPublic ReadOnly Property SelectedCells As DataGridViewSelectedCellCollection属性值
一个 DataGridViewSelectedCellCollection,表示用户选定的单元格。
- 属性
示例
下面的代码示例演示如何使用 SelectedCells 集合查找所选内容中的值之和。 在此示例中,此集合是循环访问的,并将有效值添加到总和中,该总和显示在标签中。 此示例是 事件中提供的更大示例的 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)
            {
                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
注解
无论属性值如何SelectionMode,始终SelectedCells填充属性。
此属性包含引用所选内容的只读快照。 如果保留此集合的副本,它可能与用户更改所选内容的实际后续 DataGridView 状态不同。 因此,不应对集合的副本进行操作。