有条件地更改单元格颜色

本文关键字:单元格 颜色 有条件 | 更新日期: 2023-09-27 17:59:55

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

以下是我的查询:

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

有条件地更改单元格颜色

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

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

它非常好用!