c#如何实现在datagridview中按回车键保存数据,按delete删除数据??

2025-01-07 22:02:54
推荐回答(2个)
回答1:

private void dataGridView1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)//回车键
{
string ID = dataGridView1.SelectedCells[0].Value.ToString();
string sql = string.Format("update Book set BookName='{0}' where BookID={1}", dataGridView1.SelectedCells[1].Value.ToString(), ID);
//修改语句.....
}
if (e.KeyChar == (char)16)//删除键
{
string ID = dataGridView1.SelectedCells[0].Value.ToString();

string sql = string.Format("delete Book where BookID={0} ", ID);
//删除语句.....
}

}

回答2:

private void dataGridView1_KeyUp(object sender, KeyEventArgs e)
{
if(e.KeyValue==13)
MessageBox.Show("保存数据");
else if(e.KeyValue==46)
MessageBox.Show("删除数据");
}