【问题标题】:Setting datagridview cell background color [duplicate]设置datagridview单元格背景颜色[重复]
【发布时间】:2018-06-15 00:12:48
【问题描述】:

我正在尝试检查活动列的条件是否等于 false,它会将单元格颜色行设置为红色,但即使活动列为 false,它也不会更改行颜色

这是显示的输出:

dgv_loadout.DataSource = ouput.ToList();

var activate = db.Users.ToList();
foreach (DataGridViewRow color in dgv_loadout.Rows)
{
    if (activate[color.Index].Active == false)
    {
        dgv_loadout.DefaultCellStyle.BackColor = Color.Red;
    }
}

【问题讨论】:

标签: c# winforms datagridview


【解决方案1】:

你可以试试这样的:

    private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
    {
        DataGridView dg = sender as DataGridView;

        foreach (DataGridViewRow item in dg.Rows)
        {
            //your condition
            int id = Convert.ToInt32(item.Cells[0].Value);
            if (id == 1)
            {
                dg.Rows[e.RowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.Red;
            }
        }
    }

我使用 RowsAdded 事件只是为了测试。

【讨论】:

    【解决方案2】:

    您需要为特定单元格设置BackColor。

    for(int col = 0; col < dgv_loadout.Columns.Count; col++)
    {
         [your_row].Cells[col].Style.BackColor = Color.Red;
    }
    

    如果这不起作用,请检查您的条件是否被命中。

    【讨论】:

      猜你喜欢
      • 2014-10-08
      • 1970-01-01
      • 1970-01-01
      • 2013-04-12
      • 2014-02-27
      • 2010-11-21
      • 2016-05-27
      • 2014-08-06
      • 2012-07-31
      相关资源
      最近更新 更多