【问题标题】:Datagridview Cell Backcolor formattingDatagridview 单元格背景颜色格式
【发布时间】:2014-10-08 12:16:52
【问题描述】:
 foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                string kesim = Convert.ToString(row.Cells["kesim"].Value);

                if (kesim == "True")
                {


                    dataGridView1.Rows[row.Index].Cells["kesim"].Style.BackColor = Color.Green;
                    dataGridView1.Rows[row.Index].Cells["kesim"].ReadOnly = true;
                    dataGridView1["kesim", row.Index].ReadOnly = true;
                    dataGridView1["kesim", row.Index].Style.BackColor = Color.Green;          
                 }
               else
                {

                    dataGridView1.Rows[row.Index].Cells["kesim"].Style.BackColor = Color.Red;
                    dataGridView1.Rows[row.Index].Cells["kesim"].ReadOnly = true;
                    dataGridView1["kesim", row.Index].ReadOnly = true;
                    dataGridView1["kesim", row.Index].Style.BackColor = Color.Red;      
                }
                string torna = Convert.ToString(row.Cells["torna"].Value);

                if (torna == "True")
                {


                    dataGridView1.Rows[row.Index].Cells["torna"].Style.BackColor = Color.Green;
                    dataGridView1.Rows[row.Index].Cells["torna"].ReadOnly = true;
                    dataGridView1["torna", row.Index].ReadOnly = true;
                    dataGridView1["torna", row.Index].Style.BackColor = Color.Green;
                }
              else
                {


                    dataGridView1.Rows[row.Index].Cells["torna"].Style.BackColor = Color.Red;
                    dataGridView1.Rows[row.Index].Cells["torna"].ReadOnly = true;
                    dataGridView1["torna", row.Index].ReadOnly = true;
                    dataGridView1["torna", row.Index].Style.BackColor = Color.Red;
                }
            }

我的第一行;

http://prntscr.com/4csw89

我的第二排;

http://prntscr.com/4cswco

如果单元格值为真,我想将背景颜色更改为绿色,否则为红色。

我从这个链接获取了代码;

Using a dynamic list of Custom Object and can't dynamically change dataGrid's cells properties

请帮助我的问题

【问题讨论】:

  • 使用CellFormatting 事件。
  • 你是认真的吗?我已经在使用这个了。我的代码在我的问题上
  • 您没有向我们展示您在哪里使用此代码。不要循环遍历行。只需格式化从 DataGridViewCellFormattingEventArgs 参数获得的单元格。
  • 不,您的代码中没有包含事件标头。
  • 对不起,我怎么能用 cellformatting 做到这一点?

标签: c# datagridview formatting cell


【解决方案1】:

使用 CellFormatting 事件时,您不必遍历所有行,因为该事件将为您提供正在修改的单元格的 ColumnIndex 和 RowIndex。只需格式化需要格式化的单元格:

void dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) {
  if (e.Value != null) {
    if (dgv.Columns[e.ColumnIndex].Name == "kesim" | 
        dgv.Columns[e.ColumnIndex].Name == "torna" ) {
      if (e.Value.ToString() == "True") {
        e.CellStyle.BackColor = Color.Green;
      } else {
        e.CellStyle.BackColor = Color.Red;
      }
    }
  }
}

【讨论】:

  • @MerihDursun 我该怎么处理这条评论?活动是否已上线? “不工作”是什么意思?当您使用调试器单步执行代码时会发生什么?
  • 事件成功开始结束但格式化无效
  • @MerihDursun 您的意思是代码命中了e.CellStyle.BackColor = Color 行之一,但单元格的颜色仍然没有改变?
  • 如何使用调试器单步执行代码?
  • @MerihDursun 你必须学会​​这一点。单击您希望代码停止的代码行的边缘,以显示红色圆圈。当代码点击它时,您可以使用 F8 进入每一行并通过将鼠标悬停在它们上来检查您所看到的值。
猜你喜欢
  • 2013-04-12
  • 2014-02-27
  • 2016-05-27
  • 2018-06-15
  • 2015-11-29
  • 1970-01-01
  • 2022-12-12
  • 2015-09-29
  • 2012-02-17
相关资源
最近更新 更多