【发布时间】:2016-10-20 22:13:43
【问题描述】:
我正在使用 WinForms。如果价格列有“$”符号,如何添加它的值?当列不包含“$”符号时,我可以添加值,但是当它包含时,系统会引发错误。
这是我到目前为止所拥有的。这会将价格列中的所有值相加,如果它不包含“$”符号,则将其相加。
private void sum_Btn_Click(object sender, EventArgs e)
{
Total_TxtBx.Text = (from DataGridViewRow row in dataGridView1.Rows
where row.Cells[2].FormattedValue.ToString() != string.Empty
select Convert.ToDecimal(row.Cells[2].FormattedValue)).Sum().ToString();
}
($1.00 + $2.00 + $3.00) 总文本框应等于 = $6.00
【问题讨论】:
-
为什么不在第一个 where 之后添加这个:
where !row.Cells[3]. FormattedValue. ToString(). Contains("$")? -
那些
$标志不应该是数据的一部分。该列的数据应为数字数据类型,但Format应包含$符号。
标签: c# .net winforms datagridview sum