将数量与价格相乘,显示总数

本文关键字:显示 | 更新日期: 2023-09-27 17:59:41

在网格视图中,当我单击时,我有一个编辑按钮,这两个字段将更改为文本框数量和总数。。当用户编辑时,必须自动计算总量,并在总量字段中显示结果。。但是我收到线路上的错误

decimal price= Convert.ToDecimal(row.Cells[1].Text.Replace("$",  String.Empty).Replace(",", String.Empty));

输入字符串的格式不正确这是代码

protected void txtQuantity_TextChanged(object sender, EventArgs e)
{
    TextBox txtQuantity = (TextBox)sender;
    GridViewRow row = (GridViewRow)txtQuantity.NamingContainer;
    TextBox txtTotal = (TextBox)row.FindControl("txtTotal");
    decimal price = Convert.ToDecimal(row.Cells[1].Text.Replace("$",  String.Empty).Replace(",", String.Empty));
    decimal total = price * Convert.ToInt32(txtQuantity.Text);
    txtTotal.Text = string.Format("{0:C}", total);
}

将数量与价格相乘,显示总数

试试这个:

decimal price = decimal.Parse(row.Cells[1].Text.Replace("$", "").Replace(",", ""));