如果选择了单元格,C#DataGridView将从另一列获取值

时间:2016-12-21 11:27:22

标签: c# datagridview

如果选择B列单元格,如何获得A列单元格值?

enter image description here

在这种情况下:14,24

1 个答案:

答案 0 :(得分:1)

你可以这样做

 private void datagridview1_SelectionChanged(object sender, EventArgs e)
 {
   if (datagridview1.SelectedCells.Count > 0)
   {
     foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
     {
       int selectedrowindex = cell.RowIndex;
       DataGridViewRow selectedRow = datagridview1.Rows[selectedrowindex];  
       string a = Convert.ToString(selectedRow.Cells[0].Value); 
      }
    }
  }