I have a datagridview which is editable. I have a textbox below it. I am entering the values in the grid view on run time and calculating the total in the textbox below it. The problem is, whenever i change the value in the grid view, the textbox retains the previous value and adds the new value to previous. I dont want the old value to be added. I wrote this code
private void grdPurchase_CellEndEdit_1(object sender, DataGridViewCellEventArgs e)
{
try
{
//this.grdPurchase.Refresh();
(grdPurchase.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() != "")
string value = grdPurchase.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
if (e.ColumnIndex == 4)
{
float val = float.Parse(value);
total = val;
if (vapasi1 == "Yes")
{
if((e.r
vtot += total;
txt_forvapasitotal.Text = vtot.ToString();
float vapsitot = float.Parse(txt_forvapasitotal.Text);
float vapsicalculate = (vapsitot * a);
float tax = vapsicalculate / 100;
float with_vapasi = vtot + tax;
txt_withvapasi.Text = Convert.ToString(with_vapasi);
}
else
{
nvtot += total;
txt_withoutvapasitot.Text = nvtot.ToString();
}
txt_vapasiincludedtot.Text = (float.Parse(txt_withvapasi.Text) + float.Parse(txt_withoutvapasitot.Text)).ToString();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
Try using the event DataGridView.CellValueChanged. An example can be find on MSDN. http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvaluechanged