C# 按enter键时如何从gridview窗口窗体获取行索引

C# 按enter键时如何从gridview窗口窗体获取行索引,c#,winforms,C#,Winforms,当我单击单元格(列内容)并按enter键时,下面的代码不会返回行索引 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();

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

  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();
        }
    }

在任何上下文中,您都可以引用
CurrentCell
CurrentCellAddress
来轻松获取当前行的行索引:

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

在任何上下文中,您都可以引用
CurrentCell
CurrentCellAddress
来轻松获取当前行的行索引:

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