Akshay J
Akshay J

Reputation: 5458

DataGridView Winforms Value,EdittedFormattedValue

I am making one application in which the user fills an invoice/bill using a DataGridView.

In each row of DGV, there are 4 columns:
CreditOrDebit,Customer,Credit,Debit.

CreditOrDebit can have 2 values : BY or TO 1)If it's TO the cell Credit will be readonly with a fixed value of 0.00 and Debit will be filled by user. 2)If it's BY the cell Debit will be readonly with a fixed value of 0.00 and Credit will be filled by user.

After the end of each row, credit and debit will be calculated and if credit>debit TO row will be added else BY row will be added untill credit=debit.

Now when I calculate the in the editable cell, I have to use EdittedFormattedValue else I have to use 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();

That worked perfectly. But when it comes to editing an invoice, I fill everything from database and let the use edit it.

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;

Now when I use the code that calculates credit and debit, the Cell.Value does not ever become null, so always Value is used( the new edited value appears in EdittedFormattedValue) producing wrong results.

Please suggest an appropriate solution.

Upvotes: 1

Views: 2672

Answers (1)

Renatas M.
Renatas M.

Reputation: 11820

If I understood question correctly, you should always commit user changes and always use Value instead of EditedFormatedValue in your credit and debit calculation.

EditedFormatedValue is current, formatted value of the cell, regardless of whether the cell is in edit mode and the value has not been committed.

Upvotes: 1

Related Questions