如何更改datagridview中的行颜色C# [英] How to change the row color in the datagridview C#

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

问题描述

我为datagridview中的行做错了什么不改变颜色?



我尝试了什么: < br $> b $ b

What m'I doing wrong for the row in the datagridview to not change the color?

What I have tried:

dgvArticles.DataSource = gsUtils.GetArticles();

						 //Set the color
						 foreach (DataGridViewRow row in dgvArticles.Rows)
						 {	
							 if (Convert.ToInt32(row.Cells["QuantiteStock"].Value) <= Convert.ToInt32(row.Cells["StockLimite"].Value))
							 {
								 row.DefaultCellStyle.ForeColor = Color.Red;
							 }
						 }

推荐答案

您的代码看起来不错,但也许您打算设置 BackColor



一些替代方案:

Your code looks ok, but maybe you meant to set the BackColor ?

Some alternatives:
myDataGridView.AlternatingRowsDefaultCellStyle.BackColor = Color.WhiteSmoke;



使用 CellFormatting 事件:


Using the CellFormatting event:

private void myDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (e.ColumnIndex == 1)
    {
        if (myDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == "abc")
        {
            e.CellStyle.ForeColor = Color.Red;
        }
    }
}



更多信息:如何:自定义Windows窗体DataGridView控件中的数据格式| Microsoft Docs [ ^ ]


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

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