我最近写的代码,cellendedit后,希望能将当前的表UPDATE到数据库中。
现在出现的问题,我很迷惑。
1、在我完成当前cell的编辑,点击同一个DataGridView的其他cell时,程序运转正常。
2、在我不点击这个DataGridView的其他Cell完成编辑,点击Form中另一个DataGridView时,却无法将当前修改的cell所在行插入到数据库。我已经检查了插入前DataTable中确实已经修改过了,但是Update()的返回值却是0
注:另一个DataGridView是空的,我什么也没写,卡在这里。
下面这个是cellendedit()
private void category_dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
//current cell value
Object cellValue = this.category_dataGridView.Rows[e.RowIndex].Cells[1].Value;
if (e.RowIndex >= 0 && e.RowIndex < this.category_dataGridView.Rows.Count)
{
if (!this.category_Cell_BeginEditValue.Equals(this.category_Cell_EndEditValue))
{
try
{
//after validation, push copy of the current table into stack, then update database
this.officer.Category_DataTable.Rows[e.RowIndex].SetField<string>(1, this.category_Cell_EndEditValue);
this.officer.UpDateCategoryTable();
this.redo_Undo_Helper.Undo_Opeartion_Stack.Push(this.officer.Category_DataTable.Copy());
this.category_BindingSource.DataSource = officer.Category_DataTable; }
catch (Exception ex)
{
MessageBox.Show("这就是错误!\n" + ex.ToString());
//because of undo rebind the current table to datasource
this.officer.Category_DataTable = this.redo_Undo_Helper.GetLatestCategoryTableCopy();
this.category_BindingSource.DataSource = officer.Category_DataTable;
}
}
}
}这个是Update()
public void UpDateCategoryTable()
{
this.categories_Adapter.Update(this.category_table);
this.category_table.Clear();
this.categories_Adapter.Fill(this.category_table);
}请高人解惑。。
现在出现的问题,我很迷惑。
1、在我完成当前cell的编辑,点击同一个DataGridView的其他cell时,程序运转正常。
2、在我不点击这个DataGridView的其他Cell完成编辑,点击Form中另一个DataGridView时,却无法将当前修改的cell所在行插入到数据库。我已经检查了插入前DataTable中确实已经修改过了,但是Update()的返回值却是0
注:另一个DataGridView是空的,我什么也没写,卡在这里。
下面这个是cellendedit()
private void category_dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
//current cell value
Object cellValue = this.category_dataGridView.Rows[e.RowIndex].Cells[1].Value;
if (e.RowIndex >= 0 && e.RowIndex < this.category_dataGridView.Rows.Count)
{
if (!this.category_Cell_BeginEditValue.Equals(this.category_Cell_EndEditValue))
{
try
{
//after validation, push copy of the current table into stack, then update database
this.officer.Category_DataTable.Rows[e.RowIndex].SetField<string>(1, this.category_Cell_EndEditValue);
this.officer.UpDateCategoryTable();
this.redo_Undo_Helper.Undo_Opeartion_Stack.Push(this.officer.Category_DataTable.Copy());
this.category_BindingSource.DataSource = officer.Category_DataTable; }
catch (Exception ex)
{
MessageBox.Show("这就是错误!\n" + ex.ToString());
//because of undo rebind the current table to datasource
this.officer.Category_DataTable = this.redo_Undo_Helper.GetLatestCategoryTableCopy();
this.category_BindingSource.DataSource = officer.Category_DataTable;
}
}
}
}这个是Update()
public void UpDateCategoryTable()
{
this.categories_Adapter.Update(this.category_table);
this.category_table.Clear();
this.categories_Adapter.Fill(this.category_table);
}请高人解惑。。
解决方案 »
- C#如何使用https向exe程序发送消息
- 图片滑动的算法如何实现
- RecordSet问题
- 序列化和反序列化存储一个List类型的对象的问题,请教了
- C# 服务器下载文件问题? 急急急~~~~~~~~~~~~~~
- 为什么提示添加成功,数据库却为空,急急急,快下岗
- 怎么关闭本窗口,是当有两个窗口的时候!THANK YOU!!!!
- 100分解决这个问题,给个思路也行,不够再加,谢谢
- 串口通信,频繁发送数据, ReadExisting()得到一样的数据
- 如何实现DataGrid符合一定条件的记录显示一特定颜色?
- 想在GOOGLE EARTH中添加动态模型
- vs2008中c#中怎样解决一个winform中几个按钮共用一个程序(例如 一个winform有3个退出)
{
InitializeComponent(); this.Text = StaticString.BUSSINESS_NAME + " Manage Stock"; this.category_BindingSource = new BindingSource();
this.officer = new StockOfficer();
this.redo_Undo_Helper = new Redo_Undo_Helper_ManageStockForm();
this.category_dataGridView.DataSource = this.category_BindingSource;
//push copy of the table from database to stack (original one)
this.redo_Undo_Helper.Undo_Opeartion_Stack.Push(officer.Category_DataTable.Copy());
//bind original one as data source
this.category_BindingSource.DataSource = officer.Category_DataTable;
//hide category ID
this.category_dataGridView.Columns[0].Visible = false;
this.category_dataGridView.Columns[1].Width = this.splitContainer1.Panel1.Width - 20;
}补充构造函数。
上面提到的两种奇怪现象,在点其他GRIDView来结束编辑时,datatable修改行的state是unchanged强制更改为modified时,数据库也是不作为,根本不理会。