简体   繁体   中英

Changing cell color in datagridview dynamically from an object list

Can anyone help me how can I add a colored cell in datagridview? The row info comes from an array list. If a Team color is green, I want the cell to be green too. I don't know what index I need to assign.

Here is my code:

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

I found the answer . I needed to replace the foreach with a for loop

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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