C# - 从dataGridView特定列获取最小值?

时间:2014-05-04 06:29:43

标签: c# winforms 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()));
    }
}

1 个答案:

答案 0 :(得分: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;
}