dataGridView如何根据某一列值不同颜色
来源:网络收集 点击: 时间:2024-05-17在DataGridView的RowDataBound事件里判断并修改:
if(e.Row.Cells.Text==0)
{
e.Row.Attributes.Add(bgColor, red);
}
else if(e.Row.Cells.Text500)
{
e.Row.Attributes.Add(bgColor, green);
}


在winform里,DataGridView没有RowDataBound事件,如果在winform里,如下修改:
private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
if (e.RowIndex = dataGridView1.Rows.Count - 1)
return;
DataGridViewRow dgr = dataGridView1.Rows;

try
{
if (dgr.Cells.Value.ToString() == 比较值)
{
dgr.DefaultCellStyle.ForeColor = 设置的颜色;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);

}
}
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;


private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.Value== 男 )
{
e.PaintBackground(e.CellBounds, true);
}
}

注意:
在winform里,DataGridView没有RowDataBound事件,如果在winform里,如下修改: 调用DataGridView中的RowPrePaint事件private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e){if (e.RowIndex = dataGridView1.Rows.Count - 1)return;DataGridViewRow dgr = dataGridView1.Rows;try{if (dgr.Cells.Value.ToString() == 比较值){dgr.DefaultCellStyle.ForeColor = 设置的颜色; (如:Color.red)}}catch (Exception ex){MessageBox.Show(ex.Message);}

版权声明:
1、本文系转载,版权归原作者所有,旨在传递信息,不代表看本站的观点和立场。
2、本站仅提供信息发布平台,不承担相关法律责任。
3、若侵犯您的版权或隐私,请联系本站管理员删除。
4、文章链接:http://www.1haoku.cn/art_782424.html