Click here to Skip to main content
14,695,439 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to use this code at datagridview for cell 1,2 only

<pre lang="c#"><pre> 

double vmv = 8; double vbv = 6


if (string.IsNullOrWhiteSpace(textBox1.Text)) 
            {
                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;

                }

            }


What I have tried:

 <pre>so i try this one but didn't work

<pre lang="c#">double vmv = 8

double vbv = 6;

 <pre>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."
Posted
Updated 26-Dec-17 20:58pm

1 solution

Indexes in C# start at zero: so if you have just "cell 1,2 only" then the index will be 1, not 2.

But I'd suspect you need to look at how many cells there are in the SelectedCells collection: I suspect it's not three at all.
Use the debugger to look at exactly what the data you are being passed is: we can't do that for you, we don't have access to your DGV, it's data, or the cells your user has selected!
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900