简体   繁体   中英

How to update my database after editing the datagridview in c#

I am trying to use clicking the cell of DataGridView2, then get the value of the clicked cell to edit the selected cell of DataGridView1. But, the adpter.update is failed to update the edited data to database.

This code is to get the clicked cell value of DataGridView2 to edit the selected cell value of DataGridView1.

private void DataView2_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                int TempCellIndex;
                int TempRowIndex;
                TempCellIndex = DataView1.CurrentCell.ColumnIndex;
                TempRowIndex = DataView1.CurrentCell.RowIndex;
                CellChangeContext = DataView1.CurrentCell.Value.ToString();
                try
                {
                    if (((btnFlag != 2) && (btnFlag != 3)) || (TempCellIndex < 2))
                        return;
                    DataView1.Rows[TempRowIndex].Cells[TempCellIndex++].Value = DataView2.Rows[e.RowIndex].Cells[0].Value;
                    MessageBox.Show(DataView1.Rows[TempRowIndex].Cells[TempCellIndex-1].Value.ToString());
                    if (DataView1.CurrentCell.Value.ToString().Equals(CellChangeContext))
                        return;
                    else
                        DataView1.CurrentCell.Style.ForeColor = System.Drawing.Color.Red;
                    if (CellIndex == 22)
                        DataView1.Rows[TempRowIndex].Cells[TempCellIndex - 1].Selected = false;
                    else
                    {
                        DataView1.Rows[TempRowIndex].Cells[TempCellIndex].Selected = true;
                        DataView1.CurrentCell = DataView1.Rows[TempRowIndex].Cells[TempCellIndex];
                    } 

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                };
            }

This code is to realized the update of database by clicking a button.

   private void btnSAVE_Click(object sender, EventArgs e)
        {
            if ((btnFlag == 2)||(btnFlag == 3))
            {
                DataView1.ReadOnly = true;
                EnableBtn();
                this.mST_RECIPETableAdapter.Update(this.processRecipe.MST_RECIPE);
                this.mST_RECIPETableAdapter.Fill(this.processRecipe.MST_RECIPE);
                this.DataView1.ClearSelection();
            }
            if (btnFlag == 4)
            {
                DataView2.Columns["pRNCDataGridViewTextBoxColumn"].ReadOnly = true;
                EnableBtn();
                this.mST_PROCTableAdapter.Update(this.processOption.MST_PROC);
                this.mST_PROCTableAdapter.Fill(this.processOption.MST_PROC);
            }


        }

I have solved the problem yet by myself. The key is to add a mSTRECIPEBindingSource.EndEdit(); before updating.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM