日期:2014-05-18  浏览次数:20893 次

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

请高手帮帮忙,怎样才能让所有值为1 的单元格更改背景颜色 ?


------解决方案--------------------
C# code

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

------解决方案--------------------
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