【问题标题】:Datagridview Rows change color [duplicate]Datagridview行改变颜色[重复]
【发布时间】:2014-03-01 11:36:30
【问题描述】:

我正在创建一个库存系统,它每 3 秒检查一次单元格 5 中的行,如果它低于 10。那么我的问题是如何将颜色更改为红色,使其低于 10。

private void belowstock()
    {
        int row;
        int qty, qtyOnHand;

        for (row = 0; row < dataGridView1.RowCount; row++)
        {
            qty = int.Parse(dataGridView1.Rows[row].Cells[5].Value.ToString());

            qtyOnHand = 10;
            if (qty <= qtyOnHand)
            {

                   //red  
             }     

            else
                  //white
       }
    }

【问题讨论】:

  • 你的Datagridview是否绑定了
  • 这是winforms DataGridView吗?您要更改整行颜色还是特定单元格颜色?
  • @Co.Aden 对不起先生,你说的失明是什么意思?
  • @Junaith 是的,先生。我想要单元格 5 中低于 10 的整行。
  • @DontStopLearning - 查看 Yuriy 提供的链接

标签: c# visual-studio-2010


【解决方案1】:

在您的循环中尝试以下代码:

row.DefaultCellStyle.BackColor = Color.Red;

【讨论】:

    【解决方案2】:

    LINQ

    这样做的方法

    private void belowstock()
    {
        dataGridView1.Rows.Cast<DataGridViewRow>().Where(w => (int)w.Cells[5].Value < 10).ToList().ForEach(f => f.DefaultCellStyle.BackColor = Color.Red);
        dataGridView1.Rows.Cast<DataGridViewRow>().Where(w => (int)w.Cells[5].Value > 10).ToList().ForEach(f => f.DefaultCellStyle.BackColor = Color.White);
    }
    

    只需输入此代码

    【讨论】:

    • 谢谢你的捷径先生!太棒了!
    猜你喜欢
    • 1970-01-01
    • 2012-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-07
    • 1970-01-01
    相关资源
    最近更新 更多