Click here to Skip to main content
15,921,959 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
string greenColor = "G";

OleDbCommand cmd = new OleDbCommand("Select ID from LED_DATA where color='" + greenColor + "'", con);
            OleDbDataReader reader =   cmd.ExecuteReader();

            while (reader.Read())
            {
                int r = Convert.ToInt16(reader[0].ToString());
                for (int c = 0; c < 8; c++)
                {
                   
                    dataGridView1.Rows[r].Cells[c].Style.ForeColor = Color.Green;

                    if (this.dataGridView1.ColumnCount == 7)
                    {
                        break;
                    }
                }
                
            }

error is: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index


Please help me as soon as possible.. I need it urgently.
Posted
Updated 18-Nov-13 19:55pm
v3
Comments
ArunRajendra 19-Nov-13 1:49am    
In which line are you getting this exception?
Member 10329286 19-Nov-13 4:17am    
dataGridView1.Rows[r].Cells[c].Style.ForeColor = Color.Green;
At this line i am getting exception.
Kornfeld Eliyahu Peter 19-Nov-13 1:52am    
You have only one cell! The ID one! But you try read 8 of them!!!

dataGridView1.Rows[r].Cells[c] -> c is varying from 0 to 7...

C#
for (int c = 0; c < 7; c++)

Apparently your grid has 7 columns, so in your loop you have to go from 0 to to 6.
 
Share this answer
 
Either your table does not have 8 columns, or the number of rows you are reading as the first column in each row does not match the rows count for the DataGridView.

Use the debugger: put a breakpoint on the line
C#
dataGridView1.Rows[r].Cells[c].Style.ForeColor = Color.Green;

And look at the value of r and c to check which is out of range.
 
Share this answer
 

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