如何在按下回车键时从gridview窗口中获取行索引

时间:2013-12-08 10:24:17

标签: c# winforms

当我单击单元格(列内容)并按下回车键时,下面的代码不会返回行索引。

  private void dataGridView_city_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            string grid_row = dataGridView_city.SelectedRows[0].Cells[0].Value.ToString();
        }
    }

1 个答案:

答案 0 :(得分:0)

在任何情况下,您始终可以引用CurrentCellCurrentCellAddress来轻松获取当前行的行索引:

int rowIndex = dataGridView1.CurrentCell.OwningRow.Index;
//or
int rowIndex = dataGridView1.CurrentCellAddress.Y;