我把列表显示在dataGridView1中现在想把条件符合的行的颜色设置为灰色怎么弄???

解决方案 »

  1.   

    DataGridView1_CellFormatting
    中e.CellStyle.BackColor 
    this.dataGridView1.Rows[i].DefaultCellStyle.BackColor   =   Color.Red;
    http://blog.csdn.net/ConExpress/archive/2008/11/20/3338481.aspx
      

  2.   

    DataGridView.Row[i].DefaultCellStyle.BackColor
      

  3.   

    参考
    DataGridView指定单元格颜色设定
    光标下的单元格颜色自动变换  
     [C#]
    private void DataGridView1_CellMouseEnter(object sender,
        DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
        {
            DataGridView dgv = (DataGridView)sender;
                  dgv[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.Red;
            dgv[e.ColumnIndex, e.RowIndex].Style.SelectionBackColor = Color.Red;
        }
    }private void DataGridView1_CellMouseLeave(object sender,
        DataGridViewCellEventArgs e)
    {
         if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
        {
            DataGridView dgv = (DataGridView)sender;
            dgv[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.Empty;
            dgv[e.ColumnIndex, e.RowIndex].Style.SelectionBackColor = Color.Empty;
        }
    }
      

  4.   

     private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
            {
                 if   (e.RowIndex >=   dataGridView1.Rows.Count -1 || e.RowIndex<0) 
                                    return; 
                            DataGridViewRow   dgr   =   dataGridView1.Rows[e.RowIndex]; 
                            try 
                            { 
                                    if   (dgr.Cells[ "列名"].Value.ToString()   ==   "比较的值") 
                                    {
                                        dgr.DefaultCellStyle.BackColor = Color.Gray;
                                    } 
                            } 
                            catch   (Exception   ex) 
                            { 
                                    MessageBox.Show(ex.Message); 
                            }         }
      

  5.   

    http://www.cnblogs.com/peterzb/archive/2009/05/29/1491891.html
      

  6.   

    我把问题在补充一下啊 我从后台数据库中返回DataTable付给dataGridView1。dataGridView1里面每行有个字段Ddate(日期)我想判断日期小于当前时间的就把这行的颜色换为灰色???
      

  7.   


    DataGridView根据单元格值设定单元格样式
    单元格负数情况下显示黄色,0的情况下显示红色
     [C#]
    private void DataGridView1_CellFormatting(object sender,
        DataGridViewCellFormattingEventArgs e)
    {
        DataGridView dgv = (DataGridView)sender;    if (dgv.Columns[e.ColumnIndex].Name == "Column1" && e.Value is int)
        {
            int val = (int)e.Value;
                    if (val < 0)
            {
                e.CellStyle.BackColor = Color.Yellow;
            }
            else if (val == 0)
            {
                e.CellStyle.BackColor = Color.Red;
            }
        }
    }参考这个吧!对比日期就行了