如果在數據庫中找到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] .Cells [0] = .DefaultCellStyle.BackColor Color.red;' – Fabio
沒有 .defaulcellstyle.backcolor =細胞之後color.red [0] – Daryl
對於[各個細胞](http://stackoverflow.com/a/17728171/3773066)。 – OhBeWise