【问题标题】:how I can show the sum of in a datagridview column?如何在 datagridview 列中显示总和?
【发布时间】:2011-04-16 08:03:46
【问题描述】:

我需要显示来自此datagridviewcount 列的总和,但我不知道如何获取 datagridview 中的数据。

当我点击按钮时,我想在label1 中显示94

如何做到这一点?

【问题讨论】:

  • @manhaz:欢迎来到 StackOverflow!考虑对您的其他问题的答案进行投票,最好的一个,您应该使用绿色复选标记将其标记为“已接受的答案”。这将有助于确保您获得对未来问题的良好答案!

标签: c# winforms datagridview


【解决方案1】:
int sum = 0;
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
    sum += Convert.ToInt32(dataGridView1.Rows[i].Cells[1].Value);
}
label1.Text = sum.ToString();

【讨论】:

    【解决方案2】:

    如果可以,请使用 LINQ。

      label1.Text =  dataGridView1.Rows.Cast<DataGridViewRow>()
                                       .AsEnumerable()
                                       .Sum(x => int.Parse(x.Cells[1].Value.ToString()))
                                       .ToString();
    

    【讨论】:

    • 可以将DataGridViewRow 转换为DataRow 吗?
    • 但是...但是...DataGridViewRow 上是否有Field 扩展方法?
    【解决方案3】:

    如果您的网格绑定到DataTable,我相信您可以这样做:

    // Should probably add a DBNull check for safety; but you get the idea.
    long sum = (long)table.Compute("Sum(count)", "True");
    

    如果它没有绑定到一个表,你可以很容易地做到这一点:

    var table = new DataTable();
    table.Columns.Add("type", typeof(string));
    table.Columns.Add("count", typeof(int));
    
    // This will automatically create the DataGridView's columns.
    dataGridView.DataSource = table;
    

    【讨论】:

    • 我想起了我在这里的原因......我什至从未意识到 DataTable 对象上存在Compute 函数!
    【解决方案4】:

    将总行添加到将绑定到网格的数据集合中。

    【讨论】:

      【解决方案5】:

      你可以用两个datagridview做得更好,你添加相同的数据源,隐藏第二个的标题,将第二个的高度设置为第一个的行的高度,关闭第二个的所有可调整大小的属性,同步两者的滚动条,仅水平,将第二个放在第一个的底部等。

      看看:

         dgv3.ColumnHeadersVisible = false;
         dgv3.Height = dgv1.Rows[0].Height;
         dgv3.Location = new Point(Xdgvx, this.dgv1.Height - dgv3.Height - SystemInformation.HorizontalScrollBarHeight);
         dgv3.Width = dgv1.Width;
      
         private void dgv1_Scroll(object sender, ScrollEventArgs e)
              {
                  if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
                  {
                      dgv3.HorizontalScrollingOffset = e.NewValue;
                  }
              }
      

      【讨论】:

        【解决方案6】:
         decimal Total = 0;
        
        for (int i = 0; i < dataGridView1.Rows.Count; i++)
        {
            Total+= Convert.ToDecimal(dataGridView1.Rows[i].Cells["ColumnName"].Value);
          }
        
        labelName.Text = Total.ToString();
        

        【讨论】:

          【解决方案7】:

          使用 LINQ 的快速而干净的方式

          int total = dataGridView1.Rows.Cast<DataGridViewRow>()
                          .Sum(t => Convert.ToInt32(t.Cells[1].Value));
          

          在 VS2013 上验证

          【讨论】:

            【解决方案8】:
            //declare the total variable
            int total = 0;
            //loop through the datagrid and sum the column 
            for(int i=0;i<datagridview1.Rows.Count;i++)
            {
                total +=int.Parse(datagridview1.Rows[i].Cells["CELL NAME OR INDEX"].Value.ToString());
            
            }
            string tota
            

            【讨论】:

              【解决方案9】:

              使用 LINQ 很干净

              label1.Text = (from DataGridViewRow row in datagridview1.Rows
                                               where !String.IsNullOrEmpty(row.Cells[1].FormattedValue.ToString())
                                               select Convert.ToDouble(row.Cells[1].FormattedValue)).Sum().ToString();
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2019-10-10
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2017-03-18
                相关资源
                最近更新 更多