1

我正在使用以下代码通过按钮单击获取所选数据网格视图结果的 ID(第一)列

`DataGridViewSelectedCellCollection DGV = this.dgvSearch.SelectedCells;
for (int i = 0; i <= DGV.Count - 1; i++)
{
string ID = Convert.ToString(dgvSearch.CurrentRow.Cells[0].Value);
MessageBox.Show(ID); 
}`

我在消息框中获取 ID,但与列次数相同,我只希望每个选择行一次。

4

1 回答 1

2

使用DataGridViewSelectedRowCollection而不是DataGridViewSelectedCellCollection循环选择的行数。在循环内只给出你给出的相同。替换this.dgvSearch.SelectedCellsthis.dgvSearch.SelectedRows..

更新:

 DataGridViewSelectedRowCollection DGV =this.dgvSearch.SelectedRows;
  foreach (DataGridViewRow row in DGV)
  {
 DataRow myRow = (row.DataBoundItem as DataRowView).Row;
  string ID = Convert.ToString(myRow.Cells[0].Value);
  MessageBox.Show(ID); 

   }
于 2013-11-13T14:15:42.210 回答