Column1 Column 2
Payment 2001 $45.9
Refund 2002 $56.8
Refund 2003 $67.9
Payment 2004 $88.0
Payment 2006 $39.9
您好。我有一个DataGridView
有两列。一列是文本描述,另一列是实际值。我只想总结付款值,并通过按钮点击将小数和显示为TextBox
。我上面的付款金额是173.8美元,这是我想在TextBox
中显示的内容。我已经设法凑齐了足够的代码来选择我的初始付款价值但是我从那时起就陷入了困境。 :)
任何帮助将不胜感激。到目前为止我的代码:
foreach (DataGridViewRow row in dataGridView1.Rows)
{
for (int index = 0; index < dataGridView1.ColumnCount - 1; index++)
{
DataGridViewCell cell = row.Cells[index];
if (cell.Value == DBNull.Value || cell.Value == null)
continue;
if (cell.Value.ToString().Contains("Payment"))
{
DataGridViewCell next = row.Cells[index + 1];
答案 0 :(得分:1)
<强>解决方案强>
在循环之前声明变量:
decimal sum = 0m;
然后,使用相同的代码,在循环中只需解析数量(使用适当的CultureInfo
)并在循环时添加它们:
sum += Decimal.Parse(next.Value.ToString(), NumberStyles.Currency, myCultureInfo);
示例强>
我创建了以下测试,以展示它应该如何解析任何文化。 (我无法测试GHS,因为我无法找到相关的对于GHS ,您可以创建自定义文化。下面是一个完全自定义的示例,但对于GHS,您可能只需要更改CultureInfo
,但我还是为了演示目的测试了另一个多字符。)custom.NumberFormat.CurrencySymbol
属性。
decimal amount = 1234.56m;
CultureInfo us = new CultureInfo("en-US");
CultureInfo gb = new CultureInfo("en-GB");
CultureInfo by = new CultureInfo("be-BY");
// Values for...
CultureInfo custom = (CultureInfo)us.Clone(); // USD GHS
custom.NumberFormat.CurrencyDecimalDigits = 2; // 2 2
custom.NumberFormat.CurrencyDecimalSeparator = " DECIMAL "; // "." "."
custom.NumberFormat.CurrencyGroupSeparator = " GROUP "; // "," ","
custom.NumberFormat.CurrencyGroupSizes = new int[] { 2 }; // { 3 } { 3 }
custom.NumberFormat.CurrencyNegativePattern = 0; // 0 0
custom.NumberFormat.CurrencyPositivePattern = 0; // 0 0
custom.NumberFormat.CurrencySymbol = "SYMBOL "; // "$" "₵" or "GH₵" ?
NumberFormatInfo nf1 = us.NumberFormat;
NumberFormatInfo nf2 = gb.NumberFormat;
NumberFormatInfo nf3 = by.NumberFormat;
NumberFormatInfo nf4 = custom.NumberFormat;
string s1 = amount.ToString("c", nf1);
string s2 = amount.ToString("c", nf2);
string s3 = amount.ToString("c", nf3);
string s4 = amount.ToString("c", nf4);
Console.WriteLine(s1); // $1,234.56
Console.WriteLine(s2); // £1,234.56
Console.WriteLine(s3); // 1 234,56 Br
Console.WriteLine(s4); // SYMBOL 12 GROUP 34 DECIMAL 56
decimal d1 = Decimal.Parse(s1, NumberStyles.Currency, us);
decimal d2 = Decimal.Parse(s2, NumberStyles.Currency, gb);
decimal d3 = Decimal.Parse(s3, NumberStyles.Currency, by);
decimal d4 = Decimal.Parse(s4, NumberStyles.Currency, custom);
Console.WriteLine(d1); // 1234.56
Console.WriteLine(d2); // 1234.56
Console.WriteLine(d3); // 1234.56
Console.WriteLine(d4); // 1234.56
答案 1 :(得分:0)
@OhBeWise那完美无瑕。真的很感激帮助。感谢您的耐心等待。完整代码
private void button8_Click(object sender, EventArgs e)
{
decimal amount = 0;
CultureInfo us = new CultureInfo("en-US");
CultureInfo custom = (CultureInfo)us.Clone();
custom.NumberFormat.CurrencySymbol = "GHS ";
foreach (DataGridViewRow row in dataGridView1.Rows)
{
for (int index = 0; index < dataGridView1.ColumnCount - 1; index++)
{
DataGridViewCell cell = row.Cells[index];
if (cell.Value == DBNull.Value || cell.Value == null)
continue;
if (cell.Value.ToString().Contains("Payment"))
{
DataGridViewCell next = row.Cells[index + 1];
string s4 = next.Value.ToString();
amount+= Decimal.Parse(s4, NumberStyles.Currency, custom);
textBox22.Text = amount.ToString();
}
}
}