【问题标题】:datagridView rolling average valuesdatagridView 滚动平均值
【发布时间】:2015-05-04 13:51:13
【问题描述】:

我制作了一些软件来计算数据网格内容的平均值,下面的代码完美运行,但是我只想计算第一、第二和第三,30 行数据的平均值。为此,我需要指定row.count,这可能吗?

这是我的代码:

for (int i = 0; i < dataGridView1.Rows.Count - 1; ++i) {
    sum +=Convert.ToDouble(dataGridView1.Rows[i].Cells["heartrate"].Value);
}

【问题讨论】:

标签: c# math datagridview average


【解决方案1】:

所以你想在你的循环变量中创建 Row.Count 吗?

  private double average(int firstRow,int RowsCount)
  { 
      double sum=0d;
      for (int i = firstRow; i < RowsCount; i++) 
      {
          for(int ii=0;ii<dataGridView1.Columns.Count;ii++)
          {
          sum +=Convert.ToDouble(dataGridView1[i,ii].Value);
          }
      }
      return sum/(RowsCount*dataGridView1[i].Columns.Count);
  } 

喜欢吗?还是我弄错了? (抱歉还不能发表评论。)

编辑:

如果你喜欢它更短,你当然可以试试 lambda:

 private double average(int startIDX ,int lng )
    {
        return dataGridView1.Rows.OfType<DataGridViewRow>().Where(x => x.Index >= startIDX && x.Index < startIDX + lng).Select(x => x.Cells.OfType<DataGridViewCell>().Sum(y => {try{return Convert.ToInt32(y.Value);}catch{return 0;}})).Average();
    }

这例如返回 Rows[startIDX] 和 Rows[startIDX+lng] 之间所有行的平均值。在这里,我使用“int”作为单元格值的类型... 告诉我你喜欢的类型,也许我会更好地帮助你......

【讨论】:

  • 有更简单的方法吗?只需通过 for 循环计算前 30 个?
  • 无法将“System.TimeSpan”类型的对象转换为“System.IConvertible”类型。
  • 1. “'System.TimeSpan' 输入 'System.IConvertible'”,在哪一行?
猜你喜欢
  • 1970-01-01
  • 2021-05-08
  • 2020-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-29
相关资源
最近更新 更多