从对象列表中动态更改datagridview中的单元格颜色

时间:2015-06-10 23:38:46

标签: c# datagridview

任何人都可以帮助我如何在datagridview中添加彩色单元格?行信息来自数组列表。如果团队颜色为绿色,我希望细胞也是绿色。我不知道我需要分配什么索引。

这是我的代码:

private void UC_Teams_Load(object sender, EventArgs e)
{
    TeamList = TeamsOperations.Get(null);
    dg_teams.Rows.Clear();

    foreach (Team t in TeamList)
    {
            for (int i = 0; i < TeamList.Count; i++)
            {
                if (t.Color == "RED")
                {
                    dg_teams.Rows.Add(t.ID, t.name, t.Color, t.productOwner,                 t.scrumMaster);
                    dg_teams.Rows[i].Cells[2].Style.BackColor = Color.Red;
                }

                if (t.Color == "BLUE")
                {
                    dg_teams.Rows.Add(t.ID, t.name, t.Color, t.productOwner, t.scrumMaster);
                    dg_teams.Rows[i].Cells[2].Style.BackColor = Color.Blue;
                }

                if (t.Color == "GREEN")
                {
                    dg_teams.Rows.Add(t.ID, t.name, t.Color, t.productOwner, t.scrumMaster);
                    dg_teams.Rows[i].Cells[2].Style.BackColor = Color.Green;
                }

                if (t.Color == "YELLOW")
                {
                    dg_teams.Rows.Add(t.ID, t.name, t.Color, t.productOwner, t.scrumMaster);
                    dg_teams.Rows[i].Cells[2].Style.BackColor = Color.Yellow;
                }

                if (t.Color == "ORANGE")
                {
                    dg_teams.Rows.Add(t.ID, t.name, t.Color, t.productOwner, t.scrumMaster);
                    dg_teams.Rows[i].Cells[2].Style.BackColor = Color.Orange;
                }
            }
    }
}

1 个答案:

答案 0 :(得分:1)

我找到了答案。我需要用for循环替换foreach

    for (int i = 0; i < TeamList.Count; i++)
        {
            if (TeamList[i].Color == "RED")
            {
                dg_teams.Rows.Add(TeamList[i].ID, TeamList[i].name, TeamList[i].Color, TeamList[i].productOwner, TeamList[i].scrumMaster);
                dg_teams.Rows[i].Cells[2].Style.ForeColor = Color.Red;
            }
            if (TeamList[i].Color == "GREEN")
            {
                dg_teams.Rows.Add(TeamList[i].ID, TeamList[i].name, TeamList[i].Color, TeamList[i].productOwner, TeamList[i].scrumMaster);
                dg_teams.Rows[i].Cells[2].Style.ForeColor = Color.Green;
            }
            if (TeamList[i].Color == "YELLOW")
            {
                dg_teams.Rows.Add(TeamList[i].ID, TeamList[i].name, TeamList[i].Color, TeamList[i].productOwner, TeamList[i].scrumMaster);
                dg_teams.Rows[i].Cells[2].Style.ForeColor = Color.Yellow;
            }
            if (TeamList[i].Color == "BLUE")
            {
                dg_teams.Rows.Add(TeamList[i].ID, TeamList[i].name, TeamList[i].Color, TeamList[i].productOwner, TeamList[i].scrumMaster);
                dg_teams.Rows[i].Cells[2].Style.ForeColor = Color.Blue;
            }
            if (TeamList[i].Color == "ORANGE")
            {
                dg_teams.Rows.Add(TeamList[i].ID, TeamList[i].name, TeamList[i].Color, TeamList[i].productOwner, TeamList[i].scrumMaster);
                dg_teams.Rows[i].Cells[2].Style.ForeColor = Color.Orange;
            }