擷取 Windows 表單 DataGridView 控制項中選定的儲存格、行和列

來源:互聯網
上載者:User
擷取 DataGridView 控制項中選中的儲存格
  • 使用 SelectedCells 屬性。

    注意

    使用 AreAllCellsSelected 方法可避免顯示可能具有大量資料的儲存格。

    Visual Basic 複製代碼
    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
    C# 複製代碼
    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 屬性設定為 FullRowSelect 或 RowHeaderSelect。

    Visual Basic 複製代碼
    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
    C# 複製代碼
    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 屬性設定為 FullColumnSelect 或 ColumnHeaderSelect。

    Visual Basic 複製代碼
    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
    C# 複製代碼
    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");
    }
    }

---------------------------------

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.