DataGridView 设置渲染某个行的颜色,点击列头排序以后渲染的设置没有了

刘乐钢铁 2020-09-22 08:57:19
遇到这种情况该怎么处理呢?
...全文
8910 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Antony_WU_SZ 2020-10-20
  • 打赏
  • 举报
回复
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if(dataGridView1.Columns[e.ColumnIndex].Name.Contains("Result")) { String stringValue = e.Value as string; if (stringValue!=null) { if (stringValue.ToLower().Contains("error")) { e.CellStyle.BackColor = Color.Red; } else if(stringValue.ToLower().Contains("warning")) { e.CellStyle.BackColor = Color.Yellow; } } } else if (dataGridView1.Columns[e.ColumnIndex].Name.Contains("ModuleName")) { String stringValue = e.Value as string; if (stringValue != null) { if (stringValue.ToLower().Contains("error") || stringValue.ToLower().Contains("disk")) { e.CellStyle.BackColor = Color.Red; } else if (stringValue.ToLower().Contains("warning")) { e.CellStyle.BackColor = Color.Yellow; } } } } 含特殊字符的变颜色,很简单,供参考!
刘乐钢铁 2020-10-19
  • 打赏
  • 举报
回复
非常感谢,我去试一下。
shisee922 2020-09-22
  • 打赏
  • 举报
回复
设置dataGridView1_CellPainting事件 private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { if (e.RowIndex < 0) return; DataGridViewRow row = dataGridView1.Rows[e.RowIndex]; if (true) //设置需要改变颜色的条件 { row.Cells[0].Style.BackColor = Color.Gainsboro; //设置单元格背景色 } }

110,529

社区成员

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

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

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