【问题标题】:Changing Row Color of DataGridView Based on Cell Value根据单元格值更改 DataGridView 的行颜色
【发布时间】:2023-04-06 15:41:01
【问题描述】:

我有 2 列的 Data Gridview,即 FilenameStatus。DataGridview 有一个像这样的数据源设置

 var bs = new BindingSourceAsync();
 bs.DataSource = data;
 dataGridView4.DataSource = bs;

使用异步等待方法更新状态列的值。当状态值为“无效”时,如果其“有效”,我需要将相应的行颜色更改为红色和绿色。

为此,我尝试连接到 DataGridView 的 CellValueChanged 事件

dataGridView4.CellValueChanged += DataGridView4_CellValueChanged;

但该事件从未被触发。我该如何解决这个问题。请指教。

【问题讨论】:

标签: c# .net datagridview datasource bindingsource


【解决方案1】:

是的,在网格视图的 RowDataBound 中

    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        // check if it is the DataRow not the header row
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //Cell[1] is the cell contain the value for the condition
            if (Convert.ToInt16(e.Row.Cells[1].Text) < 10)
            {
                e.Row.Cells[0].BackColor = System.Drawing.Color.Yellow;
                e.Row.Cells[1].BackColor = System.Drawing.Color.Yellow;
            }
            else if (Convert.ToInt16(e.Row.Cells[1].Text) < 30)
            {
                e.Row.Cells[0].BackColor = System.Drawing.Color.Blue;
                e.Row.Cells[1].BackColor = System.Drawing.Color.Blue;
            }
        }
    }

【讨论】:

  • 此解决方案适用于 GridView,而不是 DataGridView。此事件不存在于 DataGridView 或任何类似的东西中。
猜你喜欢
  • 1970-01-01
  • 2017-02-05
  • 2013-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-15
  • 2015-06-11
相关资源
最近更新 更多