读书人

怎么根据不同值改 DataGridView 行背景

发布时间: 2012-02-11 09:51:35 作者: rapoo

如何根据不同值改 DataGridView 行背景色。
-----------------------------
1 是 张三 男
2 否 李四 女
3 是 王二 男
4 否 麻子 男
-----------------------------


如何将性别为男的行颜色改成红色?


[解决办法]
<asp:TemplateField HeaderText = "操作区 ">
<ItemTemplate>
<div style= "background-color: <%# DataBinder.Eval(Container, "DataItem.Sex ").ToString()== "男 "? "red ": "blue " %> "

</div>
</ItemTemplate>
</asp:TemplateField>
[解决办法]

ItemDataBound事件里
if (e.Item.Cells[性别列的索引值].Text == "男 ")
e.Item.BackColor = System.Drawing.Color.Red;
if (e.Item.Cells[性别列的索引值].Text == "女 ")
e.Item.BackColor = System.Drawing.Color.Blue;
[解决办法]
我觉得在邦定事件里面写会不会好些!
[解决办法]
你可以CellPainting处理这个事件,示例如下:

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.Value== "男 ")
{
e.PaintBackground(e.CellBounds, true);
}
}
[解决办法]
学习学习~

读书人网 >C#

热点推荐