c# - DatagridVIew 单个单元格格式
问题描述
试图在这个问题上找到答案,但没有发现任何对我有用的东西。我需要根据不同的条件更改 dataGridView 中单个单元格的字体颜色(背景颜色等)。Fof 示例 - 此 dataGridView 中同一行的另一个单元格中的值。我之前找到的所有解决方案都不能为我解决这个问题。
下面是我解决这个问题的建议。
解决方案
这是我的解决方案:您需要创建事件 datagridView_cell_Formating:
并添加类似这样的代码:
private void datagridView_cell_Formating(object sender, DataGridViewCellFormattingEventArgs e)
{
if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Target_column")
{
if ((e.Value != null) && (dataGridView1.Rows[e.RowIndex].Cells["Condition_cell"].Value.ToString() == "value"))
{
e.CellStyle.ForeColor = Color.Yellow;
}
else if ((e.Value != null))
{
e.CellStyle.ForeColor = Color.Red;
}
}
}
希望这会对某人有所帮助。
推荐阅读
- javascript - 将 JSON 中的函数解析为静态值
- algorithm - 从元素的非重复组合(两个)创建数组,以便在每个集合中所有元素仅出现一次
- react-native - 我正在尝试使用 GET 方法和 Post 方法 React Native 的 AsyncStorage
- graphql - 使用 Apollo + GraphQL 将 Prisma 1 升级到 Prisma 2
- javascript - Console.log() 在给变量赋值后返回 undefined
- python - 忽略 .DS_Store 看门狗
- java - JavaFX:带有 CheckComboBoxCell 的 TreeTableView 未正确更新单元格
- python-3.x - 使用 grpcio-tools 在生成的模块导入中实现相对路径
- java - 为什么firebase无法创建新用户?
- javascript - 仅当目标对象中存在替换时如何替换字符串?