Nisa Yathu
Nisa Yathu

Reputation: 39

Datagridview Color changed wrong in C#.net

when i load the data grid view color changed wrong i applied red color if the item is active and i applied blue if the item is completed. but blue color only shown if the item is active. i attached the screenshot below

  dataGridView1.Rows.Clear();
    string sql;
    sql = "select * from repair";
    cmd = new SqlCommand(sql, con);
    con.Open();
    dr = cmd.ExecuteReader();

    int i = 0;
    double totalsub = 0;
    double totaldue = 0;
    while (dr.Read())
    {
        dataGridView1.Rows.Add(dr[0], dr[1], dr[2], dr[3], dr[4], dr[5], dr[6], dr[7], dr[8]);

        if (dr[6].ToString() == "Active")
        {
            dataGridView1.Rows[i].Cells[6].Style.BackColor = Color.Red;
        }
        else if (dr[6].ToString() == "Completed")
        {
            dataGridView1.Rows[i].Cells[6].Style.BackColor = Color.Blue;
        }  

    }

Upvotes: 1

Views: 43

Answers (1)

William
William

Reputation: 269

You forgot to increment your variable i, so you're only changing the color for the first line (index 0).

int i = 0;
...

while (dr.Read())
{
  ...
  i++;

}

Upvotes: 2

Related Questions