0

編集可能な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());
    }
}
4

2 に答える 2

1

イベントDataGridView.CellValueChangedを使用してみてください。例はMSDNにあります。http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvaluechanged

于 2012-06-21T09:20:22.860 に答える
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();
                    }
于 2012-06-21T09:22:45.293 に答える