2014-05-04 116 views
1

這是我如何將數據輸入到電網:C# - 從dataGridView特定列獲取最小值?

this.dataGridView1.Rows.Add("Room: " + label40.Text, "$" + label38.Text, type, textBox1.Text + "m", textBox2.Text + "m", label10.Text, label9.Text); 

這是我嘗試。循環:

private void button1_Click(object sender, EventArgs e) 

{ 列表成本=新列表();

foreach (DataGridViewRow row in dataGridView1.Rows) 
{ 
    if (null != row && null != row.Cells[1].Value) 
    { 

     costs.Add(Convert.ToInt32(row.Cells[1].ToString())); 
    } 
} 

回答

0

試試這個,

private void button1_Click(object sender, EventArgs e) 
{ 
    List<int> costs = new List<int>(); 

    //Iterate through each row in the grid 
    foreach (DataGridViewRow row in dataGridView1.Rows) 
    { 
     if (null != row && null != row.Cells[1].Value) 
     { 
      //Add cost value to list 
      costs.Add(Convert.ToInt32(row.Cells[1].Value.ToString())); 
     } 
    } 

    //get 50% of min value(lowest) 
    int result = costs.Min()/2; 
} 
+0

@MarcusValentino是我的錯,看到更新的答案。它應該是'row.Cells [1] .Value.ToString()' – Kurubaran