C# 如何在文本框中反映在datagridview编辑模式中所做的更改
我有一个可编辑的datagridview。我在下面有一个文本框。我在运行时的网格视图中输入值,并在下面的文本框中计算总数。问题是,每当我在网格视图中更改值时,文本框都会保留以前的值,并将新值添加到以前的值中。我不想增加旧的价值。我写了这段代码C# 如何在文本框中反映在datagridview编辑模式中所做的更改,c#,.net,C#,.net,我有一个可编辑的datagridview。我在下面有一个文本框。我在运行时的网格视图中输入值,并在下面的文本框中计算总数。问题是,每当我在网格视图中更改值时,文本框都会保留以前的值,并将新值添加到以前的值中。我不想增加旧的价值。我写了这段代码 private void grdPurchase_CellEndEdit_1(object sender, DataGridViewCellEventArgs e) { try { //this.grdPurchase.Refr
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());
}
}
尝试使用事件DataGridView.CellValueChanged。可以在MSDN上找到一个示例 将此行
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();
}
让我告诉你,很难理解你的风格。无论如何,我假设可能有来自任何早期执行周期的缓存值。我在datagridview中有多行,我想添加列中所有行的值。但如果用户更改网格中的任何值,文本框应反映新值,而不是旧值。如果我不按照u的建议添加,它将覆盖textbox的值,并最终为我提供列的最后一个值,而不是总和。在这种情况下,您是否可以修改您的逻辑,以便在您将其显示在textbox上之前进行纳税计算。在这行中进行税务计算。“txt_vapasiincludedtot.Text=(float.Parse(txt_with vapasi.Text)+float.Parse(txt_without vapasitot.Text)).ToString();”感谢s_nair,我使用了cellvaluechange事件。它起作用了……)