如何在datagridview中将前景颜色更改为指定行 [英] How can I change the fore color to specified row in datagridview

查看:82
本文介绍了如何在datagridview中将前景颜色更改为指定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

我有一个带有c#的datagridview,并且该gridview中的每一行都有状态0或1.所以我想改变整行的前级颜色,状态为1.我怎么能做到这一点。请帮我。谢谢大家。

Dear All,
I have an datagridview with c# and each row in that gridview have status 0 or 1. So I want to change fore color of entire row that have status 1. How can I do it. Please help me. Thanks you all.

推荐答案

试试这个代码



try this code

for (int k = 0; k < grd1.RowCount; k++)
     {
         if (grd1.Rows[k].Cells["status"].Value.ToString()=="1")
         {
            grd1.Rows[k].DefaultCellStyle.BackColor = Color.LightBlue;
         }
      }


如果网格中的行顺序可能会发生变化,我建议将此处理程序添加到网格 CellPainting 事件

if order of rows in the grid may changes, i suggest add this handler to grid CellPainting event
private void ContentCellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex >= 0 && e.ColumnIndex >= 0 && grid.Rows[e.RowIndex].Cells["Status"].Value.ToString() == "1")
    e.CellStyle.ForeColor = Color.Magenta;
else
    e.CellStyle.ForeColor = Color.GreenYellow;
}


这篇关于如何在datagridview中将前景颜色更改为指定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆