简体   繁体   English

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

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

I am currently working on a Program that displays and handles data from a database in C# forms.我目前正在开发一个程序,该程序以 C# 形式显示和处理来自数据库的数据。 I have a DataGridView that is empty upon Program Load, and it later filled with data from the database when the user selects items from a ToolStripMenu.我有一个在程序加载时为空的 DataGridView,稍后当用户从 ToolStripMenu 中选择项目时,它会填充数据库中的数据。 I need to be able to alter the background color of a cell in each row, every second.我需要能够每秒更改每一行中单元格的背景颜色。 I have instantiated a timer, and set it up to tick every second.我已经实例化了一个计时器,并将其设置为每秒滴答一次。 I have set up a method event to execute every time the timer ticks (every second), shown below:我设置了一个方法事件以在每次计时器滴答声(每秒)时执行,如下所示:

  void _timer_0_Tick(object sender, EventArgs e)
    {

    }

In this method I would like to be able to color the "status" cell of each row in the DataGrid to either red or green, based upon if the Status is good or bad.在这种方法中,我希望能够根据状态是好还是坏将 DataGrid 中每一行的“状态”单元格着色为红色或绿色。

Here is some pseudocode in the method illustrating what I would like to do:这是方法中的一些伪代码,说明了我想要做什么:

  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;
        }
   }

Every DataGridView row/column edit example I've seen has to use an event handler based upon the DataGridView, how can I implement the timer while also editing the GridView in real time?我见过的每个 DataGridView 行/列编辑示例都必须使用基于 DataGridView 的事件处理程序,如何在实时编辑 GridView 的同时实现计时器?

Thanks谢谢

Your pseudo-code is close - just use the proper properties:您的伪代码很接近 - 只需使用正确的属性:

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