2017-03-17 192 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

沒有工作, 但我運行時沒有錯誤,但平均時間不顯示在標籤或文本框 –