How to retrieve selected row data in datagridview in Winform?

To obtain the data of the selected row, you can use the following code:

  1. Use the property SelectedRows to retrieve the collection of selected rows.
  2. Retrieve the collection of cells in the selected row using the Cells property.
  3. Retrieve the value of each cell using the Value property.

Here is a sample code:

// 获取选中行的数据
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
    // 获取每个单元格的值
    string col1Value = row.Cells[0].Value.ToString();
    string col2Value = row.Cells[1].Value.ToString();
    // ...
    // 处理每行数据
}

Please note that the examples above assume that the first cell of each line of data is of type string. If this is not the case for you, you may need to convert it to the appropriate type.

bannerAds