datagridview中,将所有值为1的单元格更改背景,我用for循环遍历单元格,最后的结果是最后一个值为1的单元格可以更改。
设置断点调试的结果是:每次遍历到值为1的单元格,更改背景颜色,然后遍历都下一个的时候原来更改的就又变成默认颜色。请高手帮帮忙,怎样才能让所有值为1 的单元格更改背景颜色 ?

解决方案 »

  1.   


            private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            {
                if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "1")
                    e.CellStyle.BackColor = Color.Red;
            }
      

  2.   

    e.Row.Attributes.Add("onclick", "if(window.oldtr!=null){window.oldtr.runtimeStyle.cssText='';}this.runtimeStyle.cssText='background-color:#e6c5fc';window.oldtr=this");
    e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#B2DFEE'");
    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
    使用于asp.net IE
      

  3.   

     非常感谢 bdmh 大侠 ~在附加一个问题: 当我把datagridview中单元格值为1 的背景改为红色以后,然后再把背景为红色的单元格值改为null,应该怎么实现 ?
      

  4.   

    三楼:
    大侠你好,我做的不是asp~ 
    非常感谢您的回答!