سؤال

This is how I enter data into the Grid:

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

This is what I have attempted. loop:

private void button1_Click(object sender, EventArgs e)

{ List costs = new List();

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

       costs.Add(Convert.ToInt32(row.Cells[1].ToString()));
    }
}
هل كانت مفيدة؟

المحلول

Try this,

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;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top