如何:获取 Windows 窗体 DataGridView 控件中选定的单元格、行和列

更新:2007 年 11 月

您可以使用 SelectedCellsSelectedRowsSelectedColumns 属性相应地从 DataGridView 控件中获取选中的单元格、行或列。在下面的过程中,您将获取选中的单元格,并将其行索引和列索引显示在 MessageBox 中。

获取 DataGridView 控件中选中的单元格

  • 使用 SelectedCells 属性。

    说明:

    使用 AreAllCellsSelected 方法可避免显示可能具有大量数据的单元格。

    Private Sub selectedCellsButton_Click( _
        ByVal sender As Object, ByVal e As System.EventArgs) _
        Handles selectedCellsButton.Click
    
        Dim selectedCellCount As Integer = _
            dataGridView1.GetCellCount(DataGridViewElementStates.Selected)
    
        If selectedCellCount > 0 Then
    
            If dataGridView1.AreAllCellsSelected(True) Then
    
                MessageBox.Show("All cells are selected", "Selected Cells")
    
            Else
    
                Dim sb As New System.Text.StringBuilder()
    
                Dim i As Integer
                For i = 0 To selectedCellCount - 1
    
                    sb.Append("Row: ")
                    sb.Append(dataGridView1.SelectedCells(i).RowIndex _
                        .ToString())
                    sb.Append(", Column: ")
                    sb.Append(dataGridView1.SelectedCells(i).ColumnIndex _
                        .ToString())
                    sb.Append(Environment.NewLine)
    
                Next i
    
                sb.Append("Total: " + selectedCellCount.ToString())
                MessageBox.Show(sb.ToString(), "Selected Cells")
    
            End If
    
        End If
    
    End Sub
    
    private void selectedCellsButton_Click(object sender, System.EventArgs e)
    {
        Int32 selectedCellCount =
            dataGridView1.GetCellCount(DataGridViewElementStates.Selected);
        if (selectedCellCount > 0)
        {
            if (dataGridView1.AreAllCellsSelected(true))
            {
                MessageBox.Show("All cells are selected", "Selected Cells");
            }
            else
            {
                System.Text.StringBuilder sb =
                    new System.Text.StringBuilder();
    
                for (int i = 0;
                    i < selectedCellCount; i++)
                {
                    sb.Append("Row: ");
                    sb.Append(dataGridView1.SelectedCells[i].RowIndex
                        .ToString());
                    sb.Append(", Column: ");
                    sb.Append(dataGridView1.SelectedCells[i].ColumnIndex
                        .ToString());
                    sb.Append(Environment.NewLine);
                }
    
                sb.Append("Total: " + selectedCellCount.ToString());
                MessageBox.Show(sb.ToString(), "Selected Cells");
            }
        }
    }
    

获取 DataGridView 控件中选中的行

  • 使用 SelectedRows 属性。若要使用户能够选择行,您必须将 SelectionMode 属性设置为 FullRowSelectRowHeaderSelect

    Private Sub selectedRowsButton_Click( _
        ByVal sender As Object, ByVal e As System.EventArgs) _
        Handles selectedRowsButton.Click
    
        Dim selectedRowCount As Integer = _
            dataGridView1.Rows.GetRowCount(DataGridViewElementStates.Selected)
    
        If selectedRowCount > 0 Then
    
            Dim sb As New System.Text.StringBuilder()
    
            Dim i As Integer
            For i = 0 To selectedRowCount - 1
    
                sb.Append("Row: ")
                sb.Append(dataGridView1.SelectedRows(i).Index.ToString())
                sb.Append(Environment.NewLine)
    
            Next i
    
            sb.Append("Total: " + selectedRowCount.ToString())
            MessageBox.Show(sb.ToString(), "Selected Rows")
    
        End If
    
    End Sub
    
    private void selectedRowsButton_Click(object sender, System.EventArgs e)
    {
        Int32 selectedRowCount =
            dataGridView1.Rows.GetRowCount(DataGridViewElementStates.Selected);
        if (selectedRowCount > 0)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
    
            for (int i = 0; i < selectedRowCount; i++)
            {
                sb.Append("Row: ");
                sb.Append(dataGridView1.SelectedRows[i].Index.ToString());
                sb.Append(Environment.NewLine);
            }
    
            sb.Append("Total: " + selectedRowCount.ToString());
            MessageBox.Show(sb.ToString(), "Selected Rows");
        }
    }
    

获取 DataGridView 控件中选中的列

  • 使用 SelectedColumns 属性。若要使用户能够选择列,您必须将 SelectionMode 属性设置为 FullColumnSelectColumnHeaderSelect

    Private Sub selectedColumnsButton_Click( _
        ByVal sender As Object, ByVal e As System.EventArgs) _
        Handles selectedColumnsButton.Click
    
        Dim selectedColumnCount As Integer = dataGridView1.Columns _
            .GetColumnCount(DataGridViewElementStates.Selected)
    
        If selectedColumnCount > 0 Then
    
            Dim sb As New System.Text.StringBuilder()
    
            Dim i As Integer
            For i = 0 To selectedColumnCount - 1
    
                sb.Append("Column: ")
                sb.Append(dataGridView1.SelectedColumns(i).Index.ToString())
                sb.Append(Environment.NewLine)
    
            Next i
    
            sb.Append("Total: " + selectedColumnCount.ToString())
            MessageBox.Show(sb.ToString(), "Selected Columns")
    
        End If
    
    End Sub
    
    private void selectedColumnsButton_Click(object sender, System.EventArgs e)
    {
        Int32 selectedColumnCount = dataGridView1.Columns
            .GetColumnCount(DataGridViewElementStates.Selected);
        if (selectedColumnCount > 0)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
    
            for (int i = 0; i < selectedColumnCount; i++)
            {
                sb.Append("Column: ");
                sb.Append(dataGridView1.SelectedColumns[i].Index
                    .ToString());
                sb.Append(Environment.NewLine);
            }
    
            sb.Append("Total: " + selectedColumnCount.ToString());
            MessageBox.Show(sb.ToString(), "Selected Columns");
        }
    }
    

编译代码

此示例需要:

可靠编程

此主题中描述的集合在选择了大量单元格、行或列时不能有效执行。有关使用这些具有大量数据的集合的更多信息,请参见 缩放 Windows 窗体 DataGridView 控件的最佳做法

请参见

参考

DataGridView

SelectionMode

AreAllCellsSelected

SelectedCells

SelectedRows

SelectedColumns

其他资源

Windows 窗体 DataGridView 控件的选项和剪贴板使用