Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 13-Jan-13 16:25pm
Comments
jibesh at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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)
    {
        //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.
  Permalink  
v3

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 9,935
1 OriginalGriff 6,987
2 CPallini 5,845
3 George Jonsson 4,015
4 Gihan Liyanage 3,266


Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 13 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100