C#中的dataGridview控件中的数据根据条件显示颜色

pession 2013-06-20 08:04:29
各位好,我是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;
}

...全文
2404 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
请叫我卷福 2013-06-21
  • 打赏
  • 举报
回复
引用 9 楼 pession 的回复:
[quote=引用 8 楼 xiaozhi_5638 的回复:] 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["courceNetDataGridViewTextBoxColumn"].Value);
               // MessageBox.Show(intGrade.ToString())           
                if (intGrade < 60)
                {
                    this.dataGridView1.Rows[e.RowIndex].Cells["courceNetDataGridViewTextBoxColumn"].Style.ForeColor = Color.Red;//设置小于60的数字显示为红色
                    this.dataGridView1.Rows[e.RowIndex].Cells["courceNetDataGridViewTextBoxColumn"].Style.SelectionForeColor = Color.Red;
                }
            }
            
        }
[/quote] 依此类托 一般如果想要修改控件默认显示方式(颜色、字体、样式等等) 你可以注册一些类似CellPainting的事件 在事件处理程序中 根据条件(一般e参数可以获取想要的东西) 来自定义控件的显示方式 控件内部一般这样实现的: // ... DataGridViewCellPaintingEventArgs e = new DataGridViewCellPaintingEventArgs(...); if(CellPainting != null) { CellPainting(this,e); // } //接着绘制控件 //... 你要做的: 1.注册类似CellPaiting事件 2.通过e参数判断是否满足条件 3.修改显示方式
pession 2013-06-21
  • 打赏
  • 举报
回复
引用 8 楼 xiaozhi_5638 的回复:
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["courceNetDataGridViewTextBoxColumn"].Value);
               // MessageBox.Show(intGrade.ToString())           
                if (intGrade < 60)
                {
                    this.dataGridView1.Rows[e.RowIndex].Cells["courceNetDataGridViewTextBoxColumn"].Style.ForeColor = Color.Red;//设置小于60的数字显示为红色
                    this.dataGridView1.Rows[e.RowIndex].Cells["courceNetDataGridViewTextBoxColumn"].Style.SelectionForeColor = Color.Red;
                }
            }
            
        }
pession 2013-06-21
  • 打赏
  • 举报
回复
此贴终结,问题已经解决了。这里非常感谢xiaozhi_5638的回复解答,他很耐心提供了解决的思路,对待新人友好。再次感谢
请叫我卷福 2013-06-20
  • 打赏
  • 举报
回复
引用 5 楼 pession 的回复:
[quote=引用 2 楼 iceMung 的回复:] DataGridViewRow dgr = dataGridView1.Rows[3]; //假设是第三成绩,如果是2-5列的成绩呢? 先搞懂概念吧~
抱歉,打错了,是第四列[/quote]
pession 2013-06-20
  • 打赏
  • 举报
回复
引用 2 楼 iceMung 的回复:
DataGridViewRow dgr = dataGridView1.Rows[3]; //假设是第三成绩,如果是2-5列的成绩呢? 先搞懂概念吧~
抱歉,打错了,是第四列
kenei 2013-06-20
  • 打赏
  • 举报
回复
你这代码是根据第四行第四列的成绩,把整个第四行颜色都修改了. 还有我记得修改datagridview颜色方法在初始化时候调用是没用的,好像要放在form_load事件里
请叫我卷福 2013-06-20
  • 打赏
  • 举报
回复
注册 CellPainting事件 void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { } 在里面e.Value就是某一单元格的值 通过判断 设置颜色
iceMung 2013-06-20
  • 打赏
  • 举报
回复
DataGridViewRow dgr = dataGridView1.Rows[3]; //假设是第三成绩,如果是2-5列的成绩呢? 先搞懂概念吧~
stiff_neck 2013-06-20
  • 打赏
  • 举报
回复
这段代码应该没有问题,用个foreach循环遍历所有行就可以了 可能是后面的代码又把颜色设置回去了
请叫我卷福 2013-06-20
  • 打赏
  • 举报
回复
引用 7 楼 pession 的回复:
[quote=引用 6 楼 xiaozhi_5638 的回复:] [quote=引用 5 楼 pession 的回复:] [quote=引用 2 楼 iceMung 的回复:] DataGridViewRow dgr = dataGridView1.Rows[3]; //假设是第三成绩,如果是2-5列的成绩呢? 先搞懂概念吧~
抱歉,打错了,是第四列[/quote] 行[/quote] 额,学艺不精,把行和列混淆了,贻笑大方,根据你的提示,我又写了2段代码,但是对于绑定数据库的datagridview还是无法显示出我想要的红色 请你帮忙看看我的代码有误吗,第一段注释了,第二段没注释,还写了一个foreach的判断也没成功
       
   //private void dgvDataTable_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        //{
        //    if (e.ColumnIndex == this.dataGridView1.Columns["courceNetDataGridViewTextBoxColumn"].Index)//根据成绩设置单元格样式
        //    {
        //        object obj = this.dataGridView1.Rows[e.ColumnIndex].Cells[e.ColumnIndex].Value;
        //        int n = Convert.ToInt32(obj);//对不及格的成绩设置特殊样式
        //        if (n < 60)
        //        {
        //            DataGridViewCellStyle cellStyle = new DataGridViewCellStyle();
        //            cellStyle.ForeColor = Color.Red;

        //            //this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.ForeColor = Color.Red;//设置小于60的数字显示为红色
        //            //this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.SelectionForeColor = Color.Red;
        //            //this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.Alignment = DataGridViewContentAlignment.MiddleRight;
        //        }
        //    }
        //}
            private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            int intGrade = Convert.ToInt32(this.dataGridView1.Rows[e.RowIndex].Cells["courceNetDataGridViewTextBoxColumn"].Value);
            if (intGrade < 60)
            {
                this.dataGridView1.Rows[e.RowIndex].Cells[1].Style.ForeColor = Color.Red;//设置小于60的数字显示为红色
                this.dataGridView1.Rows[e.RowIndex].Cells[1].Style.SelectionForeColor = Color.Red;
                this.dataGridView1.Rows[e.RowIndex].Cells[1].Style.Alignment = DataGridViewContentAlignment.MiddleRight;
            }
        }

 //foreach (DataRow row1 in dataGridView1.Rows)
            //{
            //    DataGridViewRow gridRow = new DataGridViewRow();
            //    DataGridViewButtonCell btn_cell = new DataGridViewButtonCell();
            //    btn_cell.Value = "通过";
            //    for (int col_index = 0; col_index < dataGridView1.Columns.Count; col_index++)
            //    {
            //        DataGridViewTextBoxCell cell = new DataGridViewTextBoxCell();
            //        cell.Value = row1[col_index];
            //        gridRow.Cells.Add(cell);
            //        if (col_index == 3)
            //        {
            //            if (Convert.ToDecimal(row1[3]) < 60)
            //            {
            //                DataGridViewCellStyle cellStyle = new DataGridViewCellStyle();
            //                cellStyle.ForeColor = Color.Red;
            //                cell.Style = cellStyle;
            //                btn_cell.Value = "补考";
            //            }
            //        }
            //    }
            //    gridRow.Cells.Add(btn_cell);
            //    dataGridView1.Rows.Add(gridRow);
            //}
[/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; } } } 这个没问题 你再自己检查检查
pession 2013-06-20
  • 打赏
  • 举报
回复
引用 6 楼 xiaozhi_5638 的回复:
[quote=引用 5 楼 pession 的回复:] [quote=引用 2 楼 iceMung 的回复:] DataGridViewRow dgr = dataGridView1.Rows[3]; //假设是第三成绩,如果是2-5列的成绩呢? 先搞懂概念吧~
抱歉,打错了,是第四列[/quote] 行[/quote] 额,学艺不精,把行和列混淆了,贻笑大方,根据你的提示,我又写了2段代码,但是对于绑定数据库的datagridview还是无法显示出我想要的红色 请你帮忙看看我的代码有误吗,第一段注释了,第二段没注释,还写了一个foreach的判断也没成功
       
   //private void dgvDataTable_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        //{
        //    if (e.ColumnIndex == this.dataGridView1.Columns["courceNetDataGridViewTextBoxColumn"].Index)//根据成绩设置单元格样式
        //    {
        //        object obj = this.dataGridView1.Rows[e.ColumnIndex].Cells[e.ColumnIndex].Value;
        //        int n = Convert.ToInt32(obj);//对不及格的成绩设置特殊样式
        //        if (n < 60)
        //        {
        //            DataGridViewCellStyle cellStyle = new DataGridViewCellStyle();
        //            cellStyle.ForeColor = Color.Red;

        //            //this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.ForeColor = Color.Red;//设置小于60的数字显示为红色
        //            //this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.SelectionForeColor = Color.Red;
        //            //this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.Alignment = DataGridViewContentAlignment.MiddleRight;
        //        }
        //    }
        //}
            private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            int intGrade = Convert.ToInt32(this.dataGridView1.Rows[e.RowIndex].Cells["courceNetDataGridViewTextBoxColumn"].Value);
            if (intGrade < 60)
            {
                this.dataGridView1.Rows[e.RowIndex].Cells[1].Style.ForeColor = Color.Red;//设置小于60的数字显示为红色
                this.dataGridView1.Rows[e.RowIndex].Cells[1].Style.SelectionForeColor = Color.Red;
                this.dataGridView1.Rows[e.RowIndex].Cells[1].Style.Alignment = DataGridViewContentAlignment.MiddleRight;
            }
        }

 //foreach (DataRow row1 in dataGridView1.Rows)
            //{
            //    DataGridViewRow gridRow = new DataGridViewRow();
            //    DataGridViewButtonCell btn_cell = new DataGridViewButtonCell();
            //    btn_cell.Value = "通过";
            //    for (int col_index = 0; col_index < dataGridView1.Columns.Count; col_index++)
            //    {
            //        DataGridViewTextBoxCell cell = new DataGridViewTextBoxCell();
            //        cell.Value = row1[col_index];
            //        gridRow.Cells.Add(cell);
            //        if (col_index == 3)
            //        {
            //            if (Convert.ToDecimal(row1[3]) < 60)
            //            {
            //                DataGridViewCellStyle cellStyle = new DataGridViewCellStyle();
            //                cellStyle.ForeColor = Color.Red;
            //                cell.Style = cellStyle;
            //                btn_cell.Value = "补考";
            //            }
            //        }
            //    }
            //    gridRow.Cells.Add(btn_cell);
            //    dataGridView1.Rows.Add(gridRow);
            //}

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧