winform下:
有一个gridview用来显示数据库里面的温度信息,如何根据温度显示不同的颜色,如温度小于20时,该行的背景色为白色,温度大于20时该行为红色

解决方案 »

  1.   

    dataGridView1.Rows[0].DefaultCellStyle.ForeColor = Color.Red;
      

  2.   

    for(int i=0;i<dataGridView.Rows.Count;i++){
       if(int.Parse(dataGridView.Rows[i].Cells["wendu"].value.ToString())>20){
          dataGridView1.Rows[i].DefaultCellStyle.ForeColor = Color.Red;
       }else{
          dataGridView1.Rows[i].DefaultCellStyle.ForeColor = Color.White;
       }
    }
      

  3.   

    这个方法是可以的,但是显示的时候先显示的白色背景行,再改变行的背景颜色,我是先设置的gridview的数据源,再改变的背景色,所以就是这个效果了。有没有办法边显示行边改变行的背景颜色,
      

  4.   

    现在可以了,逐条写入的数据,边写边判断。
    我改变的是行的背景色,应该是dgvDetail.Rows[i].DefaultCellStyle.BackColor = Color.LightYellow;
      

  5.   

    这样处理不知道可行不?Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
            If e.ColumnIndex = 0 AndAlso e.RowIndex > -1 Then
                Dim cell As DataGridViewCell = Me.dgv.Rows(e.RowIndex).Cells("温度")
                Select Case cell.Value
                    Case Is > 20
                        cell.Style.BackColor = Color.Red
                    Case Is < 20
                        cell.Style.BackColor = Color.White
                End Select
            End If
        End Sub