求dataGridView中符合特定条件的整行文本字体变红变粗体

i逐浪 2018-11-22 03:05:07

现有部门信息查询一份表,要求如下:
1、当列名为“部门属性”满足为“基本生产部门”条件时,对应整行文本的字体变红变粗体,其他不变
2、使用foreach循环语句来实现

能提供完整识别语句,且能成功的答复给分!

...全文
429 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
i逐浪 2018-11-23
  • 打赏
  • 举报
回复
引用 4 楼 xuzuning 的回复:
这样写
            foreach (DataGridViewRow r in dataGridView1.Rows)
{
if ((string)(r.Cells["部门属性"].Value) == "基本生产部门")
{
r.DefaultCellStyle.Font = new Font("宋体", 12, FontStyle.Bold);
r.DefaultCellStyle.ForeColor = Color.Red;
}
}

非常棒!这个就是我要的!谢谢版本!!!
xuzuning 2018-11-23
  • 打赏
  • 举报
回复
这样写
            foreach (DataGridViewRow r in dataGridView1.Rows)
{
if ((string)(r.Cells["部门属性"].Value) == "基本生产部门")
{
r.DefaultCellStyle.Font = new Font("宋体", 12, FontStyle.Bold);
r.DefaultCellStyle.ForeColor = Color.Red;
}
}

i逐浪 2018-11-23
  • 打赏
  • 举报
回复
引用 1 楼 听雨停了 的回复:

//在DataGridView的RowPrePaint事件中加入下面的代码就可以了
if (Convert.ToString(this.dataGridView1.Rows[e.RowIndex].Cells["部门属性"].Value) == "基本生产部门")
{
this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.Font = new Font("宋体", 12, FontStyle.Bold);
this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Red;
}

这个方案是使用事件,能否不使用事件?
colouful_2021 2018-11-22
  • 打赏
  • 举报
回复
foreach(var item in dataGridView.rows){ if (Convert.ToString(this.dataGridView1.Rows[e.RowIndex].Cells["部门属性"].Value) == "基本生产部门") { this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.Font = new Font("宋体", 12, FontStyle.Bold); this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Red; } } 循环GridView的所有行数据,然后加条件判断,符合条件的设置它的默认字体属性就好了
听雨停了 2018-11-22
  • 打赏
  • 举报
回复

//在DataGridView的RowPrePaint事件中加入下面的代码就可以了
if (Convert.ToString(this.dataGridView1.Rows[e.RowIndex].Cells["部门属性"].Value) == "基本生产部门")
{
this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.Font = new Font("宋体", 12, FontStyle.Bold);
this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Red;
}

110,534

社区成员

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

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

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