Hard coding your for loop leaves you open to your index out of range exception that you experience in the linked question as well, its not a good idea to hard code your grid when you can loop on the rows and eliminate an otherwise silly way of encountering that exception.
This should work for what your trying to do i believe.
int irows = 0;
for (int i = 0; i<; datagridView.RowCount; i++)
{
if(i < 4)
{
datagridView.Rows[irows].DefaultCellStyle.ForeColor = Color.Purple;
datagridView.Rows[irows].DefaultCellStyle.BackColor = Color.RosyBrown;
}
else if(i > 3 && i < 7)
{
datagridView.Rows[irows].DefaultCellStyle.ForeColor = Color.Purple;
datagridView.Rows[irows].DefaultCellStyle.BackColor = Color.RosyBrown;
}
else
{
datagridView.Rows[irows].DefaultCellStyle.ForeColor = Color.Purple;
datagridView.Rows[irows].DefaultCellStyle.BackColor = Color.RosyBrown;
}
irows = irows + 1;
}
Also if you don't mind marking my answer as the solution for your other question:
for the first 4 row i want to give fore and back color i try this code but error shows[
^]
I see it obviously worked since you implemented it. While im at it you probably should have used the improve question functionality instead of creating a whole no question for basically the same thing as before.