按条件改变细胞颜色

时间:2017-12-24 09:53:49

标签: c# visual-studio

我想在datagridview上使用此代码仅用于单元格1,2

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;

                }

            }

所以我尝试了这个,但没有工作

double vmv = 8; //转换为double以避免错误比较字符串到字符串             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;

            }

它显示错误&#34;索引超出范围。必须是非负数且小于集合的大小。&#34;

0 个答案:

没有答案