我使用BindingSource的AddNew()方法来新增记录,结果出错.代码如下:
// 工具栏中"保存"按钮的Click Event
private void tsbSave_Click(object sender, EventArgs e)
{
if (this.currentState == interfaceState.AddState)
{
this.bsEmployees.AddNew();
}
this.bsEmployees.EndEdit();
this.sdaEmployees.Update(this.dsHrm.Tables["Employees"]);
this.currentState = interfaceState.BrowseState;
this.controlsState(this.currentState);
}
// 用户在BindingSource上调用AddNew时引发的事件
private void bsEmployees_AddingNew(object sender, AddingNewEventArgs e)
{
Hashtable newRow = new Hashtable();
newRow["E_ID"] = this.tbE_ID.Text.Trim();
newRow["E_Name"] = this.tbE_Name.Text.Trim();
newRow["E_Code"] = this.tbE_Code.Text.Trim();
newRow["E_Gender"] = this.cbE_Gender.SelectedIndex.ToString();
newRow["E_Birthday"] = this.dtpE_Birthday.Value.ToShortDateString();
newRow["E_UID"] = this.tbE_UID.Text.Trim();
newRow["E_FSchool"] = this.tbE_FSchool.Text.Trim();
newRow["E_Speciality"] = this.tbE_Speciality.Text.Trim();
newRow["E_Address"] = this.tbE_Address.Text.Trim();
newRow["E_Guardian"] = this.tbE_Guardian.Text.Trim();
newRow["E_LinkTel"] = this.tbE_LinkTel.Text.Trim();
newRow["E_InFactory"] = this.dtpE_InFactory.Value.ToShortDateString();
e.NewObject = newRow;
}错误信息(附图):http://bbs.bc-cn.net/showimg.asp?BoardID=117&filename=2006-12/200612252343724323.jpg
// 工具栏中"保存"按钮的Click Event
private void tsbSave_Click(object sender, EventArgs e)
{
if (this.currentState == interfaceState.AddState)
{
this.bsEmployees.AddNew();
}
this.bsEmployees.EndEdit();
this.sdaEmployees.Update(this.dsHrm.Tables["Employees"]);
this.currentState = interfaceState.BrowseState;
this.controlsState(this.currentState);
}
// 用户在BindingSource上调用AddNew时引发的事件
private void bsEmployees_AddingNew(object sender, AddingNewEventArgs e)
{
Hashtable newRow = new Hashtable();
newRow["E_ID"] = this.tbE_ID.Text.Trim();
newRow["E_Name"] = this.tbE_Name.Text.Trim();
newRow["E_Code"] = this.tbE_Code.Text.Trim();
newRow["E_Gender"] = this.cbE_Gender.SelectedIndex.ToString();
newRow["E_Birthday"] = this.dtpE_Birthday.Value.ToShortDateString();
newRow["E_UID"] = this.tbE_UID.Text.Trim();
newRow["E_FSchool"] = this.tbE_FSchool.Text.Trim();
newRow["E_Speciality"] = this.tbE_Speciality.Text.Trim();
newRow["E_Address"] = this.tbE_Address.Text.Trim();
newRow["E_Guardian"] = this.tbE_Guardian.Text.Trim();
newRow["E_LinkTel"] = this.tbE_LinkTel.Text.Trim();
newRow["E_InFactory"] = this.dtpE_InFactory.Value.ToShortDateString();
e.NewObject = newRow;
}错误信息(附图):http://bbs.bc-cn.net/showimg.asp?BoardID=117&filename=2006-12/200612252343724323.jpg
不能在保存的時候才AddNew,因為當你AddNew時﹐所有的綁定控件的值會使用新的記錄里面的空值作為當前值,重新綁定﹐這樣你之前輸入的內容就全部沒有了﹐根本保存不到內容。
我的"新增"方法中"断开了各文本框与数据的绑定,并清空了文本框中的值,所以将BindingSource.AddNew()写在"保存"方法中,你可以看看出问题的介面(上面有图片的地址),好象是说我AddingNew()事件中的代码有问题?查看问题的界面:http://bbs.bc-cn.net/showimg.asp?BoardID=117&filename=2006-12/200612252343724323.jpg
// 工具栏中"添加"按钮的Click Event
private void tsbAdd_Click(object sender, EventArgs e)
{
this.currentState = interfaceState.AddState;
this.clearBindingPage(); // 调用自定义方法,清除TabPage页面中各控件与数据的绑定
this.initialTabPage(); // 调用自定义方法,用除初始化TabPage页面中各控件的数据
this.controlsState(this.currentState);
}
// 工具栏中"编辑"按钮的Click Event
private void tsbEdit_Click(object sender, EventArgs e)
{
this.currentState = interfaceState.EditState;
this.controlsState(this.currentState);
}
// 工具栏中"删除"按钮的Click Event
private void tsbDelete_Click(object sender, EventArgs e)
{
}
// 工具栏中"保存"按钮的Click Event
private void tsbSave_Click(object sender, EventArgs e)
{
if (this.currentState == interfaceState.AddState)
{
this.bsEmployees.AddNew();
}
this.bsEmployees.EndEdit();
this.currentState = interfaceState.BrowseState;
this.controlsState(this.currentState);
this.bindingPage(); // 调用自定义方法,用于恢复tabPage上各控件与数据的绑定
} // 用户在BindingSource上调用AddNew时引发的事件
private void bsEmployees_AddingNew(object sender, AddingNewEventArgs e)
{
BindingSource bs = (BindingSource)sender;
DataView view = (DataView)bs.List;
DataRowView newRow = view.AddNew();
newRow["E_ID"] = this.tbE_ID.Text.Trim();
newRow["E_Name"] = this.tbE_Name.Text.Trim();
newRow["E_Code"] = this.tbE_Code.Text.Trim();
newRow["E_Gender"] = this.cbE_Gender.SelectedIndex.ToString();
newRow["E_Birthday"] = this.dtpE_Birthday.Value.ToShortDateString();
newRow["E_UID"] = this.tbE_UID.Text.Trim();
newRow["E_FSchool"] = this.tbE_FSchool.Text.Trim();
newRow["E_Speciality"] = this.tbE_Speciality.Text.Trim();
newRow["E_Address"] = this.tbE_Address.Text.Trim();
newRow["E_Guardian"] = this.tbE_Guardian.Text.Trim();
newRow["E_LinkTel"] = this.tbE_LinkTel.Text.Trim();
newRow["E_InFactory"] = this.dtpE_InFactory.Value.ToShortDateString();
e.NewObject = newRow;
}
保存完后DataView的TataTable中應該已有數據﹐但是還需要將DataTable中的新增加的數據保存到數據庫啊!如果你用的是SqlDataAdapter,記得要Update才能更新哦