有条件地改变细胞颜色

时间:2015-06-27 03:49:34

标签: c# datagridview colors cell

如果在数据库中找到DataGridView,我想更改InvoiceNo中指定单元格的单元格颜色。

以下是我的询问:

for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
    invoiceno = dataGridView1.Rows[i].Cells[0].Value.ToString();
    accpacInv = dataGridView1.Rows[i].Cells[1].Value.ToString();
    Customer = dataGridView1.Rows[i].Cells[2].Value.ToString();
    Invdate = dataGridView1.Rows[i].Cells[3].Value.ToString();
    Duedate = dataGridView1.Rows[i].Cells[4].Value.ToString();
    cur = dataGridView1.Rows[i].Cells[5].Value.ToString();
    LocAm = dataGridView1.Rows[i].Cells[6].Value.ToString();

    SqlConnection con = new SqlConnection(DbClass.StrdBase);
    con.Open();
    SqlCommand cmd = new SqlCommand("Select InvoiceNo from tblarmon     where invoiceno = '" + invoiceno  +  "'", con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    da.Fill(dt);

    if (dt.Rows.Count > 0)
    {
        // Change the Cell color of the selected cell.(if record already found ind databae)
    }
    else
    {
        con.Close();
        SaveRecordtoDB();
    }
}

1 个答案:

答案 0 :(得分:0)

得到了我自己的问题的答案。

dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.LightPink;

它完美无缺!