在datagridview中更改奇数行和偶数行的背景颜色? [英] Change backcolor of odd and even rows in datagridview?
本文介绍了在datagridview中更改奇数行和偶数行的背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在数据网格视图中有10行,我使用VS2010。
如何更改奇数和偶数行的背景颜色不同?
我尽我所能但是我有一个错误是:
索引超出范围。必须是非负数且小于集合的大小。参数名称:index
我的代码在vb.net(windows窗体)中如下:
I have 10 rows in datagrid view and I use VS2010.
How can I change a backcolor of odd and even rows different?
I try my best but I have one error that is:
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
My code is in vb.net(windows form) as follows:
Dim CountR As Integer
CountR = 0
While CountR <= DataGridView1.RowCount
If CountR Mod 2 = 0 Then
DataGridView2.Rows(CountR).DefaultCellStyle.BackColor = Color.Pink
Else
DataGridView2.Rows(CountR).DefaultCellStyle.BackColor = Color.SkyBlue
End If
CountR = CountR + 1
End While
推荐答案
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (GridView1.Rows.Count % 2 == 0)
{
e.Row.Cells[0].BackColor = System.Drawing.Color.Green;
e.Row.Cells[1].BackColor = System.Drawing.Color.Green;
//here e.Row.Cells[1] from own database
}
else
{
e.Row.Cells[0].BackColor = System.Drawing.Color.Red;
e.Row.Cells[0].BackColor = System.Drawing.Color.Red;
}
}
DataGridView
有属性AlternatingRowsDefaultCellStyle
和RowsDefaultCellStyle
。
看看他们在MSDN或网络上你会找到很多例子。
祝你好运。 :)
TheDataGridView
has propertiesAlternatingRowsDefaultCellStyle
andRowsDefaultCellStyle
.
Look them up on MSDN or the web and you'll find lots of examples.
Good luck. :)
检查DataGridView属性
Check the DataGridView properties
这篇关于在datagridview中更改奇数行和偶数行的背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文