繁体   English   中英

Datagridview以编程方式选择多行并将一列中的值求和

[英]Datagridview programmatically select multiple rows and sum up values in one column

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];

在循环之前声明一个变量:

decimal sum = 0m;

然后,使用相同的代码,在循环内只需解析出金额(使用适当的CultureInfo )并在循环时将它们加起来即可:

sum += Decimal.Parse(next.Value.ToString(), NumberStyles.Currency, myCultureInfo);

我创建了以下测试,以展示它应如何针对任何文化进行解析。 (我无法测试GHS,因为找不到相关的 CultureInfo ,但出于演示目的,我仍然测试了另一个多字符。) 对于GHS,您可以创建自定义文化。 可以在下面看到一个完全自定义的示例,尽管对于GHS,您可能只需要更改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

@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();
               }



           }


       }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM