我最近写的代码,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.   

            public ManageStock_Form()
            {
                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时,数据库也是不作为,根本不理会。
      

  2.   

    在开始更新的地方加一句this.category_dataGridView.EndEdit();