[英]How to update my database after editing the datagridview in c#
我試圖使用單擊DataGridView2的單元格,然后獲取單擊的單元格的值來編輯DataGridView1的選定單元格。 但是,adpter.update無法將編輯的數據更新到數據庫。
該代碼用於獲取單擊的DataGridView2單元格值,以編輯所選的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);
};
}
該代碼是通過單擊一個按鈕來實現數據庫的更新。
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);
}
}
我自己已經解決了這個問題。 關鍵是添加一個mSTRECIPEBindingSource.EndEdit();
在更新之前。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.