日期:2014-05-19  浏览次数:20730 次

请教:datagridview相关问题
给datagridview绑定数据后,如何根据其中的某列(该列值只有0和1)来改变行的背景色?我试过用一个循环取出某列的值,然后再根据该值改变所在行的背景色,这样是可以的,我想问问有没有更好的办法

------解决方案--------------------
学习
------解决方案--------------------
用RowPostPaint方法可以实现.
------解决方案--------------------
错了,应该是CellFormatting方法.
参考这里:http://blog.programfan.com/article.asp?id=20195
------解决方案--------------------
if(e.Row.Cells[n].Text== "0 ")
{
e.Row.Attributes.Add( "bgColor ", "red ");
}
else if(e.Row.Cells[n].Text> "500 ")
{
e.Row.Attributes.Add( "bgColor ", "green ");
}
------解决方案--------------------
另外也可以在行绑定数据后触发事件,根据某列值改变行颜色
------解决方案--------------------
该事件为GridView1_RowDataBound
------解决方案--------------------
建议参照学习http://blog.csdn.net/21aspnet/archive/2007/03/25/1540301.aspx
------解决方案--------------------
怎么把datagridview里面的数据打印成报表?
------解决方案--------------------
可以在DataGridView的RowPrePaint事件中加入代码来实现,例如根据DataGridView1的第2列值的第一个字符内容改变行的背景色为蓝色:
Private Sub DataGridView1_RowPrePaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles DataGridView1.RowPrePaint
If Not (DataGridView1.Rows(e.RowIndex).Cells(1).Value Is Nothing) Then
If Mid(DataGridView1.Rows(e.RowIndex).Cells(1).Value.ToString,1,1) = "D " Then
DataGridView1.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Blue
End If
End If
End Sub

------------------------------------------
DataGridView控件的编程技巧在人民邮电出版社出版的书《Visual Basic .NET 2005数据库编程技术与实例》一书中有更精彩的介绍,希望对你有所帮助
详情请见:http://www.ptpress.com.cn/books/Book_Information.asp?BID=16271
------------------------------------------


------解决方案--------------------
用RowPostPaint方法