【问题标题】:Show Total of column values in dataGridView在 dataGridView 中显示列值的总计
【发布时间】:2016-05-10 12:28:47
【问题描述】:

我是 C# 的新手,所以我不知道我是否能正确解决我的问题,所以请多多包涵。

我有一个名为 dgvShowAllData 的 dataGridView,它的数据源来自我的 sqlServer。有一个名为价格的列。 我想在我的 dataGridView 中的行末尾添加一个新行以显示价格列值的总和。 我尝试了多种解决方案并遇到了几个错误。 我找到了一个解决方案,总和将从 sqlServer 执行。 如下图,

Select Sum(Price) from tblProduct

但我也被困在那里。我不完全知道如何在 dataGridview 中执行两个数据源。 请告诉我一个更好的方法,这样我就可以在 dataGridView 的最后得到总数。

【问题讨论】:

  • Double result = 0; foreach (DataGridViewRow row in this.dgvShowAllData.Rows) { result += (Double)row.Cells["Price"].Value; }
  • 如何在我的数据表中添加这一行?我的代码上有这些,DataTable dt = new DataTable(); da.填充(dt);请帮忙。并感谢您的回复
  • var newRowIndex = dgvShowAllData.Rows.Add()
  • 我收到此错误,当控件绑定数据时,无法以编程方式将行添加到 dataGridView 的行集合中。
  • 如果您在数据网格中设置了数据源,则无法以编程方式添加行。你必须决定你想做什么。您可以在 sql 查询中添加一行,也可以向数据源添加其他实体,或者完全以编程方式创建数据网格而不设置数据源属性。

标签: c# sql-server winforms datagridview


【解决方案1】:

使用数据表作为 DataGridView 的数据源。然后在你的数据表中添加一个数据行,数据表有一个计算函数。

我将如何解决此问题的示例;

DataTable dt;
dt = yourDataSource; //add your data source to the Data Table
DataRow dr = dt.NewRow();
dr("Price") = dt.Compute("Sum(Price)", "");
dt.Rows.Add(dr);
dgvShowAllData.DataSource = dt;

【讨论】:

    【解决方案2】:

    在您的代码中,为您的 dataGridView 创建一个自定义页脚行,并在页脚行内的元素中填充总值。

    【讨论】:

    • 您能详细说明一下吗?
    【解决方案3】:

    Ngengis 的解决方案适用于我的实施。我的应用程序事先不知道名称、类型或列数,因此我需要对其进行一些调整。我使用了一种扩展方法(由 Dmytrii Nagirniak 编写)来检查 DataColumn.DataType 是否为数字:

        public static bool IsNumeric(this DataColumn col)
        {
            if (col == null)
                return false;
            var numericTypes = new[] { typeof(Byte), typeof(Decimal), typeof(Double), typeof(Int16), typeof(Int32), typeof(Int64), typeof(SByte), typeof(Single), typeof(UInt16), typeof(UInt32), typeof(UInt64)};
            return numericTypes.Contains(col.DataType);
        }
    

    我使用循环来创建总行。我需要捕获一个 SyntaxErrorException,以防列名不能与 Compute() 方法一起使用(带括号的聚合列失败;使用列别名可以解决该问题)

            //Total Row
            if (MakeTotalRow == true)
            {
                DataRow dr = dt.NewRow();
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    var colName = dt.Columns[j].ColumnName;
                    if (dt.Columns[j].IsNumeric()) //ensure column data can be summed
                    {
                        try
                        {
                            dr[j] = dt.Compute("Sum(" + colName + ")", "");
                        }
                        catch (SyntaxErrorException e)
                        {
                            //possible syntax error in the column name 
                        }
                    }
    
                }
                dt.Rows.Add(dr);
            }
    

    【讨论】:

      【解决方案4】:

      首先你必须使用 LINQ 实体框架,然后:

      你应该测试这些你会处理的代码,例如,你在数据库中名为“Price”的字段,所以试试:

      一步一步,

      1.从您的数据库中创建一个列表:

      List<Database.tblProduct> Lst=new List<Database.tblProduct>;
      

      所以你可以使用 C# 中的查询,

      Textblock.text=lst.Where(w=>w.PriceId==1).sum(w=>w.Price).tostring("#,##");
      

      它可以帮助你。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-04-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多