更改dataGridView单元格的前景色 [英] Changing fore color of a dataGridView cell
本文介绍了更改dataGridView单元格的前景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我要更改 DataGridViewCell
的 ForeColor如果单元格的值为0,则
为红色。
我需要包含此值的单元格而不是整行单元格(仅一个单元格)。
提前致谢。
解决方案
private void ColorRows()
{
foreach(dataGridViewRow dataGridViewTest.Rows中的行)
{
int value = Convert.ToInt32(row.Cells [0] .Value);
row.DefaultCellStyle.BackColor = GetColor(value);
}
}
private Color GetColor(int value)
{
Color c = new Color();
if(value == 0)
c = Color.Red;
返回c;
}
private void dataGridViewTest_RowsAdded(object sender,DataGridViewRowsAddedEventArgs e)
{
ColorRows();
}
试试这个
我的提示:) [ ^ ]
尝试
private void dataGridView1_CellValueChanged( object sender, DataGridViewCellEventArgs e)
{
int colIndex = e.ColumnIndex;
int rowIndex = e.RowIndex;
if (rowIndex > = 0 && colIndex > = 0 )
{
DataGridViewRow theRow = dataGridView1.Rows [rowIndex];
if (theRow.Cells [colIndex] .Value.ToString()== < span class =code-string> 0) // 检查单元格值等于0
theRow.DefaultCellStyle.ForeColor = Color.Red; // 更改单元格的ForeColor
}
}
Hi all,
I want to change a DataGridViewCell
's ForeColor
to Red if the value of the cell is "0".
I need it for the cell containing this value not the whole row cells (one cell only).
Thanks in advance.
解决方案
private void ColorRows() { foreach (DataGridViewRow row in dataGridViewTest.Rows) { int value = Convert.ToInt32(row.Cells[0].Value); row.DefaultCellStyle.BackColor = GetColor(value); } } private Color GetColor(int value) { Color c = new Color(); if (value == 0) c = Color.Red; return c; } private void dataGridViewTest_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e) { ColorRows(); }
try this
my tips :)[^]
Try
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) { int colIndex = e.ColumnIndex; int rowIndex = e.RowIndex; if (rowIndex >= 0 && colIndex >= 0) { DataGridViewRow theRow = dataGridView1.Rows[rowIndex]; if (theRow.Cells[colIndex].Value.ToString() == "0") // Check Cell Value is equal to 0 theRow.DefaultCellStyle.ForeColor = Color.Red; // Change ForeColor of Cell } }
这篇关于更改dataGridView单元格的前景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文