C# DataGridView Winforms值,EditedFormattedValue

C# DataGridView Winforms值,EditedFormattedValue,c#,winforms,datagridview,C#,Winforms,Datagridview,我正在制作一个应用程序,其中用户使用DataGridView填写发票/账单 在DGV的每一行中,有4列: 贷方息税前利润,客户,贷方,借方 CreditOrDebit可以有两个值:BY或TO 1) 如果是,则该单元格的贷项将是只读的,固定值为0.00,而借方将由用户填写。 2) 如果是通过单元格进行的,则借方将为只读,固定值为0.00,贷方将由用户填写 在每行结束后,将计算贷方和借方,如果贷方>借方到行,则将添加其他行,直到贷方=借方 现在,当我计算可编辑单元格中的值时,我必须使用Editted

我正在制作一个应用程序,其中用户使用DataGridView填写发票/账单

在DGV的每一行中,有4列:
贷方息税前利润,客户,贷方,借方

CreditOrDebit可以有两个值:BY或TO 1) 如果是,则该单元格的贷项将是只读的,固定值为0.00,而借方将由用户填写。 2) 如果是通过单元格进行的,则借方将为只读,固定值为0.00,贷方将由用户填写

在每行结束后,将计算贷方和借方,如果贷方>借方到行,则将添加其他行,直到贷方=借方

现在,当我计算可编辑单元格中的值时,我必须使用EdittedFormattedValue,否则我必须使用Value

decimal credit = 0;
decimal debit = 0;

foreach (DataGridViewRow row in dataGridViewReceipts.Rows)
{
   if (row.Cells[2].Value != null)
      credit += decimal.Parse(row.Cells[2].Value.ToString());
   else
      credit += decimal.Parse(row.Cells[2].EditedFormattedValue.ToString());


   if (row.Cells[3].Value != null)
      debit += decimal.Parse(row.Cells[3].Value.ToString());
   else
      debit += decimal.Parse(row.Cells[3].EditedFormattedValue.ToString());
}
labelCredit.Text = credit.ToString();
labelDebit.Text = debit.ToString();
这非常有效。但当涉及到编辑发票时,我会填写数据库中的所有内容,并让用户编辑发票

dataGridViewReceipts.Rows[dataGridViewReceipts.Rows.Count - 1].Cells["ColumnDirection"].Value = "BY";
dataGridViewReceipts.Rows[dataGridViewReceipts.Rows.Count - 1].Cells["ColumnParty"].Value = dr["PartyName"].ToString();
dataGridViewReceipts.Rows[dataGridViewReceipts.Rows.Count - 1].Cells["ColumnParty"].Tag = dr["PartyID"].ToString();
dataGridViewReceipts.Rows[dataGridViewReceipts.Rows.Count - 1].Cells["ColumnCredit"].Value = dr.GetDecimal(dr.GetOrdinal("Amount"));
dataGridViewReceipts.Rows[dataGridViewReceipts.Rows.Count - 1].Cells["ColumnCredit"].ReadOnly = false;
dataGridViewReceipts.Rows[dataGridViewReceipts.Rows.Count - 1].Cells["ColumnDebit"].Value = "0.00";
dataGridViewReceipts.Rows[dataGridViewReceipts.Rows.Count - 1].Cells["ColumnDebit"].ReadOnly = true;
现在,当我使用计算贷方和借方的代码时,Cell.Value永远不会变为null,因此总是使用Value(新编辑的值显示在EditdFormattedValue中)生成错误的结果


请建议一个合适的解决方案。

如果我正确理解了这个问题,您应该始终提交用户更改,并在您的信用和借记计算中始终使用Value而不是editedformattedvalue


EditedFormattedValue是单元格的最新格式化值,无论单元格是否处于编辑模式且值是否尚未提交

如果我正确理解了问题,您应该始终提交用户更改,并在您的贷记和借记计算中始终使用value而不是EditedFormattedValue


EditedFormattedValue是单元格的当前格式化值,无论单元格是否处于编辑模式且值尚未提交

我当时完全误解了您的问题。请提供一个更清楚的示例说明问题所在。我当时完全误解了您的问题。请提供一个更清楚的示例说明问题所在。是否有任何方法提交单元格编辑后自动提交?尝试单元格值更改事件或设置cell.value=cell.editedFormattedText。是否有任何方法在单元格编辑后自动提交?尝试单元格值更改事件或设置cell.value=cell.editedFormattedText。