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);
//删除语句.....
}
}
private void dataGridView1_KeyUp(object sender, KeyEventArgs e)
{
if(e.KeyValue==13)
MessageBox.Show("保存数据");
else if(e.KeyValue==46)
MessageBox.Show("删除数据");
}