你可以画用油画定制DataGridView的行和单元格。它用 ,DataGridView.RowPostPaint Event和DataGridView.RowPrePaint Event完成。
另一个期望是Paint Event
。
private void dataGridViewProducts_Paint(object sender, PaintEventArgs e)
{
foreach (DataGridViewRow row in dataGridViewProducts.Rows)
{
int value = Convert.ToInt32(row.Cells["Quantity"].Value);
if (value == 0)
row.DefaultCellStyle.BackColor = Color.Red;
}
}
您可以使用DataGridViewRowPostPaintEventArgs或DataGridViewRowPrePaintEventArgs设置的条件的基础上,行或单元格样式..
可以单独使用或与RowPrePaint
事件组合处理此事件定制在控件中出现rows
。您可以paint
整个行自己,或油漆行的特定部分,并使用DataGridViewRowPostPaintEventArgs
类的以下方法来绘制其他部分:
DRAWFOCUS
PaintCells
PaintCellsBackground
PaintCellsContent
PaintHeader
检查MSDN链接这个例子,并尝试把你的代码,这些事件之一..在这里,你,你会使用DataGridViewRowPostPaintEventArgs
int value = Convert.ToInt32(dataGridViewProducts.Rows[e.RowIndex].Cells["Quantity"].Value);
if (value == 0)
dataGridViewProducts.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;
得到Currrent行索引编辑: 把你的代码放在表单加载事件或DataBinding完成的事件。这可以解决你的问题。
试着把我的代码放在** Form_Load **方法中,但没有任何反应。我会尝试** Paint **或** PrePaint **事件。我不明白的是,** DataGridView **的** Paint **或** PrePaint **事件将使用** DefaultCellStyle.BackColor **绘制单元格的颜色。所以,如果我改变属性的值** BackColor **为什么我必须自己处理** Paint **事件? – user990692 2012-02-18 14:53:27
兄弟你是否试过** Row ** PostPaint,因为它的功能完美 – 2012-02-18 15:01:22
没有我的朋友,但我会尝试。我没有尝试过。感谢您的帮助:)) – user990692 2012-02-18 16:20:37