c# - 如何用不同的颜色初始化数据网格中的某些行?

标签 c# winforms c#-4.0

我想根据特定条件将我的 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/

相关文章:

c# - Entity Framework 在不同集合中多次引用同一个对象

c# - 如何使用 C# 模拟 CTRL+V 击键(粘贴)

wpf - 使用 WPF 和数据绑定(bind)将文件拖放到应用程序窗口中

c# - ASP.NET 中的动态代码

c# - 使用 WinForms 抽象表单

c# - 如何使用 C# 获取当前事件窗口的标题?

c# - 有没有办法获取常用对话框按钮的本地化名称?

jquery - 具有多格式 View 的MVC 3中的Ajax发布

c# - 列表<类>() 与列表<类>(0)

c# - 在通用函数中键入特定行为