经测试,代码无问题。
int s = Convert.ToInt16(dataGridView1.Rows[i].Cells[1].Value.ToString());
这句你判断一下为null 的情况跳过循环。 public Form1()
{
InitializeComponent(); dataGridView1.Rows.Add("1", "30", "30", "30");
dataGridView1.Rows.Add("1", "40", "70", "40");
dataGridView1.Rows.Add("1", "70", "60", "30");
} public void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells[1].Value == null) continue;
int s = Convert.ToInt16(dataGridView1.Rows[i].Cells[1].Value.ToString()); if (s > 60)
{ this.dataGridView1.Rows[i].Cells[1].Style.BackColor = Color.Red;
}
}
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells[1].Value == null) continue;
int m = Convert.ToInt16(dataGridView1.Rows[i].Cells[2].Value.ToString()); if (m > 60)
{ this.dataGridView1.Rows[i].Cells[2].Style.BackColor = Color.Red; } }
}
int s = Convert.ToInt16(dataGridView1.Rows[i].Cells[1].Value.ToString());
这句你判断一下为null 的情况跳过循环。 public Form1()
{
InitializeComponent(); dataGridView1.Rows.Add("1", "30", "30", "30");
dataGridView1.Rows.Add("1", "40", "70", "40");
dataGridView1.Rows.Add("1", "70", "60", "30");
} public void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells[1].Value == null) continue;
int s = Convert.ToInt16(dataGridView1.Rows[i].Cells[1].Value.ToString()); if (s > 60)
{ this.dataGridView1.Rows[i].Cells[1].Style.BackColor = Color.Red;
}
}
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells[1].Value == null) continue;
int m = Convert.ToInt16(dataGridView1.Rows[i].Cells[2].Value.ToString()); if (m > 60)
{ this.dataGridView1.Rows[i].Cells[2].Style.BackColor = Color.Red; } }
}
解决方案 »
- 为什么Application_AcquireRequestState过滤在IIS无效?
- 关于异步等待的问题,求求大家指点下
- net remoting中二进制传输问题
- 在窗体上点击检查框时激发CheckedChanged事件,但是在窗体的load事件中执行代码“checkBox1.Checked = true;”也会自动激发检查框的CheckedChanged事件,这是我所不希望的,如何避免自动激发检查框的Checked
- 下面程序中为什么会出现 "当前上下文中不存在名称“Response”"这样的错误提示?
- winform中用户控件如何返回给主form一个值? 多谢大侠!
- 关于DataGrid主从表的问题
- 100分求助,如何用C#實現在Excel檔案中插入一個文件(VB.NET已解決)
- 怎么样使winform中datagrid的表头固定
- 怎么用sql语句新建一个用户啊?
- C# Socket客户端 多态服务器的问题.
- 请问vs2010里面有没有一键折叠这些大括号和 #region #endregion
而如果你表格里根本没有设置格式化字符串,这个事件应该根本没执行
不是这的问题,但是我搞错了continue的定义是==null,跳过此次循环,继续执行下次循环的意思,见笑了。
既然不报错,你断点调试,看代码确实执行了吗?
没执行就别纠结为什么不显示了