我想询问一行如何根据dataGridView
中列的值自动更新其字体的颜色。
例如,一个表有4列:id, name, rentPayMent and check
。
检查每一行是否有check == 0
的任何值
如果是,则此行的字体color = red
Else do nothing
在运动中,我使用如下代码,但它带来错误
对象引用未设置为对象的实例,System.NullReferenceException未处理
private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[3].Value.ToString() == "0") //**Object reference not set to an instance of an object**
{
row.DefaultCellStyle.BackColor = Color.Red; //then change row color to red
}
}
}
谢谢大家,我已经得到了解决方案。
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (dataGridView1.Rows[e.RowIndex].Cells[3].Value != null && !string.IsNullOrWhiteSpace(dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString()))
{
if (dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString().Trim() == "0")
dataGridView1.Rows[e.RowIndex].DefaultCellStyle = new DataGridViewCellStyle { ForeColor = Color.Red };
}
else
{
dataGridView1.Rows[e.RowIndex].Cells[3].Style = dataGridView1.DefaultCellStyle;
}
}
答案 0 :(得分:1)
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null && !string.IsNullOrWhiteSpace(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()))
{
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style = new DataGridViewCellStyle { ForeColor = Color.Orange, BackColor = Color.Blue };
}
else
{
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style = dataGridView1.DefaultCellStyle;
}
}
答案 1 :(得分:0)
我在winform上做到了。
尝试下面的代码:
private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells["YourDatagridColumnName"].Value.ToString() == "0") //if check ==0
{
row.DefaultCellStyle.BackColor = Color.Red; //then change row color to red
}
}
}
最好的问候
答案 2 :(得分:0)
我使用类似的东西:
private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if(!string.IsNullOrEmpty(row.Cells[3].Value.ToString())) // make sure the value is present
(
if (row.Cells[3].Value.ToString() == "0")
{
row.DefaultCellStyle.BackColor = Color.Red; //then change row color to red
}
)
}
}
答案 3 :(得分:0)
优先选择命名dataGridView中的列。例如,将列命名为colCheck。 使用CellFormatting方法更改行字体颜色,如下所示
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.RowIndex > -1 && e.ColumnIndex == dataGridView1.Columns["colCheck"].Index)
{
if (e.Value != null)
{
if (dataGridView1.Rows[e.RowIndex].Cells["colCheck"].Value.ToString() == "1")
{
for (int i = 0; i < this.dataGridView1.Columns.Count; i++ )
{
this.dataGridView1.Rows[e.RowIndex].Cells[i].Style.ForeColor = Color.Red;
}
}
else
{
for (int i = 0; i < this.dataGridView1.Columns.Count; i++ )
{
this.dataGridView1.Rows[e.RowIndex].Cells[i].Style.ForeColor = Color.Black;
}
}
}
}
}
答案 4 :(得分:-1)
我个人会使用JavaScript来处理这个逻辑。但是如果将它放在CodeBehind中是绝对必须的,我会检查该单元格的值是否为“0”。如果是这样,我会添加一个“红色”(或您选择的任何名称)的CssClass,然后编写CSS以获得颜色值。这至少可以使维护更容易,而不是让服务器端处理颜色。