设置datagridview某些列或某些单元格的格式,为什么不起作用?

msnexplorer 2010-01-07 10:23:00
设置datagridview某些列或某些单元格的格式,为什么不起作用?
例如
dgv[1, 1].Style.BackColor = Color.Red;//背景色没有变成红色,不起作用
dgv.Rows[3].DefaultCellStyle.BackColor = Color.red; //背景色没有变成红色,不起作用
dgv.Columns[2].DefaultCellStyle.BackColor = Color.red; //起作用,背景变成红色;

为什么对列有效果,对单元格及行不起作用呢
...全文
442 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
titth 2010-04-27
  • 打赏
  • 举报
回复

我觉得1楼的挺有道理的

楼主可以试一试
古今多少事 2010-04-26
  • 打赏
  • 举报
回复
写在CellPainting事件中,可能事件不适合。
lt201101 2010-04-26
  • 打赏
  • 举报
回复
是啊,我也是同样效果!
zhanghuaisme 2010-01-08
  • 打赏
  • 举报
回复
经过我的实验,你所说的都可以使背景变色,有效果,可能是你在别处又进行了别的操作引起的,也可能是你的vs出问题了,也可能是缓存引起的。
private void Form1_Load(object sender, EventArgs e)
{

dataGridView1[1, 1].Style.BackColor = Color.Red;
dataGridView1.Rows[2].DefaultCellStyle.BackColor = Color.Red;
}
wuyq11 2010-01-07
  • 打赏
  • 举报
回复
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if(e.RowIndex != -1 && e.ColumnIndex == 1 && e.Value != null && e.Value.ToString() == "")
{
e.CellStyle.BackColor = Color.Red;
e.CellStyle.ForeColor = Color.Blue;
e.CellStyle.SelectionBackColor = Color.BlueViolet;
}
}

private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
DataGridViewRow dgr= dataGridView1.Rows[e.RowIndex];
if(dgr.Cells["列名"].Value.ToString()=="")
{
dgr.DefaultCellStyle.ForeColor=Color.Blue;
}
}

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧