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

获取 DataGridView 控件中选中的单元格
  • 使用 SelectedCells 属性。

    note注意

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

    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 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 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 object
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭