简体   繁体   English

C#根据内容在datagridview中设置行背景颜色

[英]C# set row background color in a datagridview depending on the content

I am using the below code to call a specific data from the db.我正在使用以下代码从数据库调用特定数据。

private void textBox25_TextChanged(object sender, EventArgs e)
    {
        con.Close();
        con.Open();
        SqlDataAdapter SDA = new SqlDataAdapter("select * from V_CustomerUnits2  where unitmodel like N'%" + textBox25.Text + "%'", con);
        DataTable DATA = new DataTable();
        SDA.Fill(DATA);
        dataGridView4.Rows.Clear();
        foreach (DataRow item in DATA.Rows)
        {
            int n = dataGridView4.Rows.Add();
            dataGridView4.Rows[n].Cells[0].Value = "false";
            dataGridView4.Rows[n].Cells[1].Value = item["fixid"].ToString();
            dataGridView4.Rows[n].Cells[2].Value = item["CompanyName"].ToString();
            dataGridView4.Rows[n].Cells[3].Value = item["unitmodel"].ToString();
            dataGridView4.Rows[n].Cells[4].Value = item["unitserial"].ToString();
            dataGridView4.Rows[n].Cells[5].Value = item["manufacturer"].ToString();
            dataGridView4.Rows[n].Cells[6].Value = item["problemdesc"].ToString();
            dataGridView4.Rows[n].Cells[7].Value = item["technotes"].ToString();
            dataGridView4.Rows[n].Cells[8].Value = item["dateaddedunit"].ToString();
            dataGridView4.Rows[n].Cells[9].Value = item["datefixed"].ToString();
            dataGridView4.Rows[n].Cells[10].Value = item["total"].ToString();
            dataGridView4.Rows[n].Cells[11].Value = item["invoicestatus"].ToString();
            dataGridView4.Rows[n].Cells[12].Value = item["invoicen"].ToString();
            Countrecords.Enabled = true;
        }
    }

I need the back color of each row to be based on a specific result.我需要每行的背景颜色基于特定结果。

For example, if the unit is repaired, color is green, if not repaired red, if cannot fix orange.例如,如果单元被修复,则颜色为绿色,如果未修复为红色,如果不能修复为橙色。

Any help?有什么帮助吗?

Use the DefaultCellStyle.BackColor property on the row than you want.在行上使用DefaultCellStyle.BackColor属性而不是你想要的。 Example:例子:

dataGridView4.Rows[n].DefaultCellStyle.BackColor = Color.Green;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM