如何突出显示Datagridview CELL而不是ROW。 [英] How to Highlight Datagridview CELL not a ROW.
本文介绍了如何突出显示Datagridview CELL而不是ROW。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,下面的代码突出显示整行,但我只需要突出显示特定的CELL。
Hello, Below code highlight the entire row, but i need to highlight only specific CELL.
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
int colIndex = e.ColumnIndex;
int rowIndex = e.RowIndex;
if (rowIndex >= 0 && colIndex >= 0)
{
DataGridViewRow theRow = dataGridView1.Rows[rowIndex];
if (theRow.Cells[colIndex].Value !=null)
{
if (theRow.Cells[colIndex].Value.ToString() == "Normal")
{
theRow.DefaultCellStyle.BackColor = Color.Green;
theRow.DefaultCellStyle.ForeColor = Color.White;
theRow.DataGridView.DefaultCellStyle.Font = new Font("Calibri", 10);
}
else if (theRow.Cells[colIndex].Value.ToString() == "Critical")
{
theRow.DefaultCellStyle.BackColor = Color.Tomato;
theRow.DefaultCellStyle.ForeColor = Color.White;
}
else if (theRow.Cells[colIndex].Value.ToString() == "Very Critical")
{
theRow.DefaultCellStyle.BackColor = Color.DarkRed;
theRow.DefaultCellStyle.ForeColor = Color.White;
}
}
推荐答案
首先,该测试永远不会有效:
First off, that test will never work:
theRow.Cells[colIndex].Value.Equals(null)
转换为:
Translates to:
Object o;
o.Equals(null)
如果被测试的对象为null,那么根据定义,没有实例可以调用Equals方法on,因为它不能是静态的,null值总是会抛出一个execption,然后才能尝试测试null值!
你可以试试:
If the object being tested is null, then there is by definition no instance to call the Equals method on, and since it cannot be static, a null value will always throw an execption, before it can try to test for a null value!
You could try with:
theRow.Cells[colIndex].Value == null
其中有更好的工作机会!
Which stands a better chance of working!
这篇关于如何突出显示Datagridview CELL而不是ROW。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文