How to get the selected row in a datagridview in C#.

To access the selected row in a DataGridView, you can achieve it by following these steps:

  1. Use the SelectedRows property of the DataGridView to obtain a collection of selected rows.
  2. Use the SelectedCells property of the DataGridView to access the collection of selected cells in order to obtain the selected rows.
  3. Use the CurrentRow property of the DataGridView to get the currently selected row.

Here is an example code:

// 获取选中的行
DataGridViewRow selectedRow = dataGridView1.SelectedRows[0];

// 获取选中的行的某一列的值
string cellValue = selectedRow.Cells["ColumnName"].Value.ToString();

// 获取当前选中的行
DataGridViewRow currentRow = dataGridView1.CurrentRow;

// 获取当前选中的行的某一列的值
string currentCellValue = currentRow.Cells["ColumnName"].Value.ToString();

With the above code, you can obtain the selected row and the value of a specific column in a DataGridView.

bannerAds