对于前4行我想要给出前后颜色我尝试此代码,但错误显示 [英] for the first 4 row i want to give fore and back color i try this code but error shows

查看:51
本文介绍了对于前4行我想要给出前后颜色我尝试此代码,但错误显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在前4行的数据网格视图中,我想给出前后颜色。



代码如下;我尝试这个代码,但错误显示;

In data grid view for the first 4 row i want to give fore and back color.

The code as follows; i try this code but error shows;

int iRow = 0;
for (int i = 0; i < 4; i++)
{
   datagridView.Rows[iRow].DefaultCellStyle.ForeColor = Color.blue;
   datagridview.rows[iRow].DefaultCellStyle.Backcolor = color.bisque;
   iRow = iRow + 1;
}





当我运行错误时显示索引超出范围。必须是非负数且小于集合的大小。

参数名称:index。



来自我的代码是什么错误。请从上面的代码中纠正我的错误。请帮助我。我试过它不能正常工作。



谢谢;



/编辑添加预标签作者:Jibesh

推荐答案

改变你的代码



Change your code from

for (int i = 0; i < 4; i++)











To

for (int i = 0; i < datagridView.RowCount; i++)





看起来你的数据网格视图中没有行,你假设会有总是4行。执行.RowCount将使用数据网格视图中的行数并循环遍历,处理任何其他逻辑以在for循环中执行前四行。



完整代码片段





It looks as though your data grid view has no rows in it and you are making the assumption that there will always be 4 rows. Doing .RowCount will use the number of rows in your data grid view and loop through that, handle any other logic to do first four rows within for loop.

Full snippet

int iRow = 0;
for (int i = 0; i < datagridView.RowCount; i++)
{
datagridView.Rows[iRow].DefaultCellStyle.ForeColor = Color.blue;
datagridview.rows[iRow].DefaultCellStyle.Backcolor = color.bisque;
iRow = iRow + 1;
}


这篇关于对于前4行我想要给出前后颜色我尝试此代码,但错误显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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