C# 在c中通过按键将datagridview转换为文本框#
//我想通过按Enter键从datagridview填充文本框。我使用了此代码但失败。还有其他选择吗C# 在c中通过按键将datagridview转换为文本框#,c#,C#,//我想通过按Enter键从datagridview填充文本框。我使用了此代码但失败。还有其他选择吗 Char ENTERKEY = (Char)Keys.Enter; private void dataGridView2_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == ENTERKEY) { int I = d
Char ENTERKEY = (Char)Keys.Enter;
private void dataGridView2_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == ENTERKEY)
{
int I = dataGridView2.CurrentRow.Index;
{
dataGridView2.CurrentRow.Selected = true;
cmbProducts.Text = dataGridView2.SelectedRows[0].Cells[0].Value.ToString();
txtPrice.Text = dataGridView2.SelectedRows[0].Cells[3].Value.ToString();
txtStock.Text = dataGridView2.SelectedRows[0].Cells[2].Value.ToString();
txtsalerate.Text = dataGridView2.SelectedRows[0].Cells[3].Value.ToString();
}
}
}
Enter
键的默认操作是将当前对象移动到下一行。如果使用按键
事件,则保留此默认行为。如果要覆盖它,需要使用KeyDown
事件并添加e.SuppressKeyPress=true例如,txtPrice.Text=dataGridView2..CurrentRow.Cells[3].Value.ToString()代码>。如果使用按键事件,CurrentRow
将是下一个事件。