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

解决方案 »

  1.   

    错了,应该是CellFormatting方法.
    参考这里:http://blog.programfan.com/article.asp?id=20195
      

  2.   

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

  3.   

    建议参照学习http://blog.csdn.net/21aspnet/archive/2007/03/25/1540301.aspx
      

  4.   

    怎么把datagridview里面的数据打印成报表?
      

  5.   

    可以在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
    ------------------------------------------------------------------------------