【问题标题】:change cell background color in winform datagrid C#在winform datagrid C#中更改单元格背景颜色
【发布时间】:2012-10-02 08:10:17
【问题描述】:

我想根据 c# windows 应用程序中的值更改背景数据网格单元格。例如,如果单元格值为 3 单元格背景颜色设置为蓝色如果单元格值等于 2 单元格背景颜色更改为红色。

【问题讨论】:

    标签: c# winforms datagrid


    【解决方案1】:

    您可以使用 CellFormatting 事件:

    private void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        if (e.ColumnIndex == 1)
            {
                if ((int)e.Value == 3)
                    e.CellStyle.BackColor = Color.Blue;
                if ((int)e.Value == 2)
                    e.CellStyle.BackColor = Color.Red;
            }
    }
    

    【讨论】:

      【解决方案2】:

      你想要这样的东西

      foreach (DataGridViewRow row in dataGridView1.Rows)
      {
          if (row.Cells[someColumnIndex].Value == 3)
              row.Cells[someColumnIndex].Style.BackColor = System.Drawing.Color.Blue;
          else if (row.Cells[someColumnIndex].Value == 2)
              row.Cells[someColumnIndex].Style.BackColor = System.Drawing.Color.Red;
      }
      

      我希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 2021-04-06
        • 2018-09-26
        • 2011-09-12
        • 2015-05-14
        • 1970-01-01
        • 2011-04-03
        • 2013-01-28
        • 1970-01-01
        • 2020-06-02
        相关资源
        最近更新 更多