读书人

datagridview选中单元个更改背景颜色,

发布时间: 2012-04-28 11:49:53 作者: rapoo

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

读书人网 >C#

热点推荐