Click here to Skip to main content
15,886,795 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i tried for the another 4 rows it is not working.please help me.

for the first 4 rows it is working fore and Back color.

Then for another 4 rows[4 to 7 rows] i want to set different fore color and back color.

Code as follows;


For the First 4 rows code as follows;

int irows = 0;

for (int i = 0; i<; datagridView.RowCount; i++)
{
datagridView.Rows[irows].DefaultCellStyle.ForeColor = Color.Purple;
datagridView.Rows[irows].DefaultCellStyle.BackColor = Color.RosyBrown;
irows = irows + 1;
}


for the another 4 rows code as follows [row no 4 to 7]


int irows1 = 4;

for(int i =4; i<7; i++)
{
datagridView.Rows[irows1].DefaultCellStyle.ForeColor = Color.Blue;
datagridView.Rows[irows1].DefaultCellStyle.BackColor = Color.Aqua;
irows = irows + 1;
}

i tried the above code for another 4 rows [row 4 to 7];

but only for the 4 th row color changing but not for the remaining rows 5 to 7.it is not changing.please help me.

from my above code,what is the mistake please rectify.i tried but is not working.
Posted
Comments
Jibesh 13-Jan-13 22:55pm    
Please do not post the same question again instead use 'Improve Question' widget to improve or modify your question. Duplicate http://www.codeproject.com/Questions/527215/forplustheplusfirstplus4plusrowplusipluswantplusto

1 solution

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.

C#
int irows = 0;

for (int i = 0; i<; datagridView.RowCount; i++)
{
    if(i < 4)
    {
        //Color rows 0,1,2,3
        datagridView.Rows[irows].DefaultCellStyle.ForeColor = Color.Purple;
        datagridView.Rows[irows].DefaultCellStyle.BackColor = Color.RosyBrown;
    }
    else if(i > 3 && i < 7)
    {
        //Color rows 4,5,6
        datagridView.Rows[irows].DefaultCellStyle.ForeColor = Color.Purple;
        datagridView.Rows[irows].DefaultCellStyle.BackColor = Color.RosyBrown;
    }
    else
    {
        //give color to all rows  > 7
        datagridView.Rows[irows].DefaultCellStyle.ForeColor = Color.Purple;
        datagridView.Rows[irows].DefaultCellStyle.BackColor = Color.RosyBrown;
    }

    //Not sure what this is needed for in the first place
    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.
 
Share this answer
 
v3

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