日期:2014-05-17  浏览次数:21079 次

C#中的dataGridview控件中的数据根据条件显示颜色
本帖最后由 pession 于 2013-06-20 08:05:35 编辑
各位好,我是C#初学者,向大家请教一下练习作业的问题
C#中的dataGridview控件中的数据颜色显示
绑定数据库中的学生成绩
显示学生成绩表时,对成绩<60用红色字体显示
>60的分级用星星显示
查找了多方资料,多次修改都不能根据成绩显示颜色,请大神帮忙看看
 
 DataGridViewRow dgr = dataGridView1.Rows[3]; //假设是第三列成绩,如果是2-5列的成绩呢?
            object obj=dgr.Cells[3].Value;
            int i;
            i=Convert.ToInt32(obj);
            if (i < 60)
            {
                dataGridView1.Rows[3].DefaultCellStyle.ForeColor = Color.Red;
            }

    

------解决方案--------------------
引用:
Quote: 引用:



        private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.Value != null)
            {
                string s = e.Value.ToString();
                if (s == "111")
                {
                    dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.ForeColor = Color.Red;
                }
            }
        }
这个没问题  你再自己检查检查

感谢你,这个方法是对的,只是没有在设计页面datagridview事件中绑定,我更改了代码显示成功了
 private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
           // int eindex = e.RowIndex + 1;
            if (e.RowIndex > -1)
            {
                int intGrade = Convert.ToInt32(this.dataGridView1.Rows[e.RowIndex].Cells["courceNetDataGridVi