0

i want to use this code at datagridview for cell 1,2 only

double vmv = 8; double vbv = 6;

            if (string.IsNullOrWhiteSpace(textBox1.Text)) // to avoid error when textbox empty
            {
                MessageBox.Show("Please Enter Number");
            }

            else
            {

                if (Convert.ToDouble(textBox1.Text) >= vmv )
                {

                    textBox1.BackColor = Color.Red;
                }

                else if (Convert.ToDouble(textBox1.Text) <= vbv)
                {
                    textBox1.BackColor = Color.Blue;

                }

                 else 
                {
                    textBox1.BackColor = Color.White;

                }

            }

so i try this one but didnt work

double vmv = 8; // convert to double to avoid error compare string to string double vbv = 6;

         if (Convert.ToDouble(dataGridView1.SelectedCells[2].Value) >= vmv )
            {

                dataGridView1.SelectedCells[2].Style.BackColor = Color.Red;
            }

         else if (Convert.ToDouble(dataGridView1.SelectedCells[2].Value) <= vbv)
            {
                dataGridView1.SelectedCells[2].Style.BackColor = Color.Blue;

            }

it show me error "Index was out of range. Must be non-negative and less than the size of the collection."

  • 1
    You don't have 3 selected cells in that case. – rene Dec 24 '17 at 09:56
  • 2
    Possible duplicate of [Index was out of range. Must be non-negative and less than the size of the collection parameter name:index](https://stackoverflow.com/questions/19162248/index-was-out-of-range-must-be-non-negative-and-less-than-the-size-of-the-colle) – rene Dec 24 '17 at 09:57
  • `dataGridView1.SelectedCells` represents all the selected cells. `dataGridView1.SelectedCells[2]` is third selected cell. So if you there are not at least 3 cells selected you will get this error. Also to compare values you need to have proper cell selected. – Chetan Dec 24 '17 at 10:53

0 Answers0