2017-03-17 190 views
0

获得平均时间我有一个DataGridView和我列时间 我需要在此的dataGridView 这是我的代码得到平均的所有时间来平均水平,但它不工作我怎样才能从DataGridView

public void avghold() 
    { 

     double[] AVG_HLOD = new double[dataGridView1.Rows.Count]; 
     AVG_HLOD = (from DataGridViewRow row in dataGridView1.Rows 
        where row.Cells["Avg_Hold"].FormattedValue.ToString() 
         != string.Empty 
        select Convert.ToDouble(row.Cells["Avg_Hold"].FormattedValue)).ToArray(); 
     double Avghold = AVG_HLOD.Average(); 
     label17.Text = AVG_HLOD.Average().ToS 
    } 

and this the image from the column

这个错误显示,当我点击运行 Please click here

感谢所有,

+0

为什么它不起作用?你看到什么错误? – confusedandamused

+0

没有错误,但我没有看到标签或文本框中的平均时间 –

+0

单步执行此功能时 - 是否正确抓取列中的值并成功转换它们?我会从那里开始,按照平均值来更新单元格。 – confusedandamused

回答

0

试试这个。

double avg = yourDataGridView.Rows.Cast<DataGridViewRow>() 
    .AsEnumerable() 
    .Average(x => int.Parse(x.Cells["Avg_Hold"].Value.ToString())); 
+0

没有工作, 但我运行时没有错误,但平均时间不显示在标签或文本框 –