繁体   English   中英

在 Timer Tick C# 表单上更改 DataGrid 单元格的背景颜色

[英]Change Background Color of DataGrid cell upon Timer Tick C# forms

我目前正在开发一个程序,该程序以 C# 形式显示和处理来自数据库的数据。 我有一个在程序加载时为空的 DataGridView,稍后当用户从 ToolStripMenu 中选择项目时,它会填充数据库中的数据。 我需要能够每秒更改每一行中单元格的背景颜色。 我已经实例化了一个计时器,并将其设置为每秒滴答一次。 我设置了一个方法事件以在每次计时器滴答声(每秒)时执行,如下所示:

  void _timer_0_Tick(object sender, EventArgs e)
    {

    }

在这种方法中,我希望能够根据状态是好还是坏将 DataGrid 中每一行的“状态”单元格着色为红色或绿色。

这是方法中的一些伪代码,说明了我想要做什么:

  void _timer_0_Tick(object sender, EventArgs e)
   {
      bool isDataStable = //code to determine if stable on my end
      if(isDataStable == true)
        {
            DataGrid.Row[Index].Column["Status"].BackColor = Green;
        }
      else
        {
            DataGrid.Row[Index].Column["Status"].BackColor = Red;
        }
   }

我见过的每个 DataGridView 行/列编辑示例都必须使用基于 DataGridView 的事件处理程序,如何在实时编辑 GridView 的同时实现计时器?

谢谢

您的伪代码很接近 - 只需使用正确的属性:

void _timer_0_Tick(object sender, EventArgs e)
   {
      bool isDataStable = //code to determine if stable on my end
      if(isDataStable == true)
        {
            DataGrid.Rows[Index].Cells["Status"].Style.BackColor = Color.Green;
        }
      else
        {
            DataGrid.Rows[Index].Cells["Status"].Style.BackColor = Color.Red;
        }
   }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM