如何将焦点重新放在编辑的最后一个单元格上?

时间:2014-08-06 02:11:05

标签: c# datagridview .net-4.0

我正在保存绑定的datagridview,在事件中,我正在使用此方法保存记录:

void dataGridView1_RowValidated(object sender, DataGridViewCellEventArgs e)
{
  try
  {
  Entity.SaveChanges();
  }
  catch (Exception ex)
  {
    MessageBox.Show(string.Format("Error while saving. Check the current row for errors.{0}{0}Exception:{0}{0}{1}", Environment.NewLine, ex.Message));
   // I'd like to push it back to the last cell edited here
  }
}

如果发生异常,我想将用户焦点推回到编辑的最后一个单元格。这可能吗?

有办法做到这一点吗?

1 个答案:

答案 0 :(得分:0)

string tbname;    
void dataGridView1_RowValidated(object sender, DataGridViewCellEventArgs e)
{
   try
{      
     Entity.SaveChanges();
     tbname = Entity.Accessible.Name;        //save the name of the last edit textbox       
}

 catch (Exception ex)
{
    MessageBox.Show(string.Format("Error while saving. Check the current row for errors.{0}{0}Exception:{0}{0}{1}", Environment.NewLine, ex.Message));

     tbname.focus();               //will return the focus to that textbox last edited    
  }
}
相关问题