不好意思,请问诸位:如何才能使datagridview中显示的指定文字变色?比如有一张表,里面有未完成的事情和已经完成的事情,我想让完成的事情显示蓝色,未完成的事情显示红色,这应该怎么做呢?
下面是一个例表:
序号         事情         状态100         晨跑         完成
101          早餐         完成
102         午餐        未完成
103    午休        未完成
104    晚餐        未完成

解决方案 »

  1.   

    datagridview 通过 DataGridViewCellStyle 对象来实现
    private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
      {
      if(e.RowIndex != -1 && e.ColumnIndex == 1 && e.Value != null && e.Value.ToString() == "")
      {
      e.CellStyle.BackColor = Color.Red;
      e.CellStyle.ForeColor = Color.Blue;
      e.CellStyle.SelectionBackColor = Color.BlueViolet;
      }
      }
      

  2.   

    private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        if (e.RowIndex >= data_table.Rows.Count) return;//初始化、选中空行时不执行
        if (data_table.Rows[e.RowIndex][2] == "完成")
        {
            e.CellStyle.BackColor = Color.Blue;
        }
        else
        {
            e.CellStyle.BackColor = Color.Red;
        }
    }
      

  3.   

    楼主,建议你不要用这些控件了,自己根据html来写,更好一些,控件用太多了,没什么进步的,for循环输出html想做什么样,做什么样,不是很好
      

  4.   

    用了一个自己程序里的变量,囧。换一个通用点的写法。private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        if (e.RowIndex >= data_table.Rows.Count) return;//初始化、选中空行时不执行
        if (dataGridView1.Rows[e.RowIndex].Cells[2].Value == "完成")
        {
            e.CellStyle.BackColor = Color.Blue;
        }
        else
        {
            e.CellStyle.BackColor = Color.Red;
        }
    }
      

  5.   

    sorry,本人是C#新手,不是很明白你的意思
      

  6.   

    按照你的方法,结果是全部都变成了红色背景,调试了一下,它跳过了  
       e.CellStyle.BackColor = Color.Blue;
    这句语句。。