【发布时间】:2015-09-14 02:27:48
【问题描述】:
如果在数据库中找到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; -
单元格[0]后没有.defaulcellstyle.backcolor= color.red
-
此外,实现
IDisposable的类的实例,如SqlConnection、SqlCommand和SqlDataAdapter应按照this example 包装在using语句中。
标签: c# datagridview colors cell