对于前4行,它从第4行到第7行在数据网格视图中设置不同的前后颜色 [英] for the first 4 row it is working from row 4 to 7 set different fore and back color in data grid view

查看:62
本文介绍了对于前4行,它从第4行到第7行在数据网格视图中设置不同的前后颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了另外4行它不能正常工作。请帮助我。



表示前4行和后退颜色。



那么另外4行[4到7行]我想设置不同的前色和背色。



代码如下;





前4行代码如下;



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;

}




另外4行代码
如下[第4行至第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试过上面的代码另外4行[第4行到第7行];



但仅适用于第4行颜色变化,但不适用于剩余行5至7.但不会改变。请帮助我。



来自我上面的代码,有什么错误请整理。我试过但是没有用。

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.

推荐答案

对你的for循环进行硬编码会让你对链接问题中遇到的索引超出范围异常,当你可以在行上循环时对硬编码你的网格不是一个好主意。消除一种遇到异常的愚蠢方式。



这应该适用于我相信你想做的事情。


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;
}







此外,如果您不介意将我的答案标记为解决方案对于你的另一个问题:对于我要提供前后颜色的前4行我尝试此代码,但错误显示 [ ^ ]



我看到它实现后显然有效。虽然即时通讯你可能应该使用改进的问题功能,而不是创建一个完整的问题基本上与以前相同。




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.


这篇关于对于前4行,它从第4行到第7行在数据网格视图中设置不同的前后颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆