2016-08-24 8 views
0

DataGridView2のセルをクリックして、DataGridView1の選択したセルを編集するためにクリックしたセルの値を取得しようとしています。しかし、adpter.updateは、編集されたデータをデータベースに更新することができません。CでDataGridViewを編集した後でデータベースを更新する方法

このコードは、DataGridView1の選択されたセル値を編集するためにDataGridView2のクリックされたセル値を取得します。

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); 
       }; 
      } 

このコードは、ボタンをクリックしてデータベースの更新を実現するためのコードです。

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); 
      } 


     } 

答えて

0

私はまだこの問題を自分で解決しています。重要な点は、更新する前にmSTRECIPEBindingSource.EndEdit();を追加することです。

関連する問題