我想根据特定条件将我的 DataGridView 的一些行初始化为红色。问题是,我一直在玩,但当显示 DataGridView 时我无法让它工作。 。我尝试在 MainForm 的构造函数中执行此操作,但根本没有运气。
private void UpdateSoldOutProducts ()
{
for (int i = 0; i < productsTable.Rows.Count; i++)
if ((int)productsTable.Rows [i] ["Quantity"] == 0)
dataGridViewProducts.Rows [i].DefaultCellStyle.BackColor = Color.Red;
}
此方法在 MainForm 的构造函数中调用。
最佳答案
尝试 RowPostPaint 事件,它对我有用:
private void dataGridViewProducts_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
if ((int)dataGridViewProducts.Rows[e.RowIndex].Cells["Quantity"].Value == 0)
dataGridViewProducts.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;
}
关于c# - 如何用不同的颜色初始化数据网格中的某些行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9339258/