0
我正在尝试使用单击DataGridView2的单元格,然后获取单击单元格的值以编辑DataGridView1的选定单元格。但是,adder.update无法将编辑的数据更新到数据库。如何在c编辑datagridview后更新我的数据库#
此代码用于获取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);
}
}