【问题标题】:Datagridview programmatically select multiple rows and sum up values in one columnDatagridview 以编程方式选择多行并对一列中的值求和
【发布时间】:2015-06-28 12:56:16
【问题描述】:
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];

【问题讨论】:

    标签: c# datagridview


    【解决方案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。这对我来说效果很好。但是我注意到如果我将美元符号更改为另一种货币,比如 GBP 或 GHS,那么我会在字符文字错误中得到太多字符。我尝试使用“”而不是“”,但没有运气。我得到一个隐式转换错误
    • @Gonja 很好。对于许多其他货币类型,该方法将失败,因为货币符号可能是多个字符。我根据这个额外的要求做了更多的研究,并找到了一种更好的方法来解析数字量。我还发现最好转换为decimal 而不是double。我已更新我的答案以反映这些变化。
    • 感谢@OhBeWise 的帮助。你的例子很有帮助。我目前正在使用 GHS 货币,所以我自己做了一些研究,但没有找到任何关于它的 cultreinfo。我正在尝试使用不变的文化来解决问题,但我从下面的行中得到一个输入字符串格式不正确的错误。有任何想法吗? sum += Decimal.Parse(next.Value.ToString(),NumberStyles.Currency, CultureInfo.InvariantCulture);
    • @Gonja 您收到该错误是因为next.Value.ToString() 可能看起来不像:¤1,234.56,这是我使用InvariantCulture 时的输出。无论如何,我相信使用定制的NumberFormat 会起作用。我已经更新了我的示例来演示。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-10
      • 1970-01-01
      • 2021-08-02
      • 1970-01-01
      • 2019-04-10
      • 2011-10-19
      • 2017-01-30
      • 1970-01-01
      相关资源
      最近更新 更多