在C#中,可以使用以下代码获取DataGridView中选中行的数据:
// 获取选中行的索引int selectedRowIndex = dataGridView1.SelectedCells[0].RowIndex;// 根据索引获取选中行的数据DataGridViewRow selectedRow = dataGridView1.Rows[selectedRowIndex];// 获取选中行的数据string value1 = selectedRow.Cells[0].Value.ToString(); // 第一列数据string value2 = selectedRow.Cells[1].Value.ToString(); // 第二列数据// ...上述代码中,dataGridView1 是你的DataGridView控件的名称。根据需要,你可以使用SelectedCells属性获取选中单元格的集合,然后通过索引获取选中行的索引,最后使用Rows属性获取选中行的DataGridViewRow对象。
注意:为了避免出现空引用异常,你应该先检查是否有选中行或选中单元格。

