如何在文本框中反映datagridview的编辑模式所做的更改

时间:2012-06-21 09:16:13

标签: c# .net

我有一个可编辑的datagridview。我下面有一个文本框。我在运行时输入网格视图中的值,并在其下方的文本框中计算总数。问题是,每当我更改网格视图中的值时,文本框都会保留先前的值并将新值添加到上一个值。我不希望添加旧值。我写了这段代码

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());
    }
}

2 个答案:

答案 0 :(得分:1)

尝试使用事件DataGridView.CellValueChanged。可以在MSDN上找到一个示例。 http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvaluechanged

答案 1 :(得分:0)

将此行vtot += total修改为vtot = total

                   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();
                    }