private void Cource_Load(object sender, EventArgs e)
{
//fill dataset by using .GetAllCourses() function
DataSet ds = new DataAccess.newCourcesDAC().GetAllCourses();
//define and set BindingSource to tblCourses of dataset
BindingSource BS = new BindingSource();
BS.DataSource = ds;
BS.DataMember = "tblCourses";
//bind datagridview to Bindingsource
ds.Tables["tblCourses"].RowChanging += new DataRowChangeEventHandler(Cource_RowChanging);
dataGridView1.DataSource = BS;
//bind for texbox to navigat 4 column of Cource table
txtCourseID.DataBindings.Add("Text", BS, "CourseID");
txtCourseName.DataBindings.Add("Text", BS, "CourseName");
txtPrequest.DataBindings.Add("Text", BS, "Prequest");
txtCourseContent.DataBindings.Add("Text", BS, "CourseContent");
}
**void Cource_RowChanging(object sender, DataRowChangeEventArgs e)
{
if ( e.Action==DataRowAction.Add)
{
if (((int)e.Row["CourseID", DataRowVersion.Proposed]) < 10)
{
e.Row.SetColumnError("CourseID", "cource id must < 10");
e.Row.CancelEdit();
}
}
}**
我有一个数据集(ds)和一个表(tblCourse),它有4列,使用绑定源绑定到4个文本框。我想通过 RowChanging 事件向数据表添加新记录时验证数据。
当发生指定条件时,我想用 [ e.Row.CancelEdit();] 取消行。
但我收到此错误:无法在 OnRowChanging 事件中调用 CancelEdit()。抛出异常以取消此更新。