如何自动为dataGridView C中的某个单元格着色#

时间:2016-06-23 10:38:38

标签: c# database datagridview colors cell

我正在寻找一种方法,在从数据库中检索数据后,使用包含“X”的文本为单元格着色。我用它的代码为整行着色。如何为包含“X”的单元格制作它?

这是我到目前为止的代码:

foreach (DataRow item in dt.Rows)
{
    int n = dataGridView1.Rows.Add();
    dataGridView1.Rows[n].Cells[0].Value = item["Timee"].ToString();
    dataGridView1.Rows[n].Cells[1].Value = item["CarColorNumber"].ToString();
    dataGridView1.Rows[n].Cells[2].Value = item["Interior"].ToString();
    dataGridView1.Rows[n].Cells[3].Value = item["Exterior"].ToString();
    dataGridView1.Rows[n].Cells[4].Value = item["CPlastic"].ToString();
    dataGridView1.Rows[n].Cells[5].Value = item["MPlastic"].ToString();
    dataGridView1.Rows[n].Cells[6].Value = item["SPlastic"].ToString();
    dataGridView1.Rows[n].Cells[7].Value = item["PlasticB"].ToString();
    dataGridView1.Rows[n].Cells[8].Value = item["WashExt"].ToString();
    dataGridView1.Rows[n].Cells[9].Value = item["WashEng"].ToString();
    dataGridView1.Rows[n].Cells[10].Value = item["WashTrunk"].ToString();
    dataGridView1.Rows[n].Cells[11].Value = item["WashSeats"].ToString();
    dataGridView1.Rows[n].Cells[12].Value = item["SeatsRmv"].ToString();
    dataGridView1.Rows[n].Cells[13].Value = item["SeatsFit"].ToString();
    dataGridView1.Rows[n].Cells[14].Value = item["Notes"].ToString();
}

for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
    if (dataGridView1.Rows[i].Cells[2].Value.ToString().Contains("    X"))
    {
        dataGridView1.CurrentRow.Cells[2].Style.BackColor = Color.GreenYellow;
    }
    if (dataGridView1.Rows[i].Cells[3].Value.ToString().Contains("    X"))
    {
        dataGridView1.CurrentRow.Cells[3].Style.BackColor = Color.GreenYellow;
    }
    if (dataGridView1.Rows[i].Cells[4].Value.ToString().Contains("    X"))
    {
        dataGridView1.CurrentRow.Cells[4].Style.BackColor = Color.GreenYellow;
    }
    if (dataGridView1.Rows[i].Cells[5].Value.ToString().Contains("    X"))
    {
        dataGridView1.CurrentRow.Cells[5].Style.BackColor = Color.GreenYellow;
    }
    if (dataGridView1.Rows[i].Cells[6].Value.ToString().Contains("    X"))
    {
        dataGridView1.CurrentRow.Cells[6].Style.BackColor = Color.GreenYellow;
    }
    if (dataGridView1.Rows[i].Cells[7].Value.ToString().Contains("    X"))
    {
        dataGridView1.CurrentRow.Cells[7].Style.BackColor = Color.GreenYellow;
    }
    if (dataGridView1.Rows[i].Cells[8].Value.ToString().Contains("    X"))
    {
        dataGridView1.CurrentRow.Cells[8].Style.BackColor = Color.GreenYellow;
    }
    if (dataGridView1.Rows[i].Cells[9].Value.ToString().Contains("    X"))
    {
        dataGridView1.CurrentRow.Cells[9].Style.BackColor = Color.GreenYellow;
    }
    if (dataGridView1.Rows[i].Cells[10].Value.ToString().Contains("    X"))
    {
        dataGridView1.CurrentRow.Cells[10].Style.BackColor = Color.GreenYellow;
    }
    if (dataGridView1.Rows[i].Cells[11].Value.ToString().Contains("    X"))
    {
        dataGridView1.CurrentRow.Cells[11].Style.BackColor = Color.GreenYellow;
    }
    if (dataGridView1.Rows[i].Cells[12].Value.ToString().Contains("    X"))
    {
        dataGridView1.CurrentRow.Cells[12].Style.BackColor = Color.GreenYellow;
    }
    if (dataGridView1.Rows[i].Cells[13].Value.ToString().Contains("    X"))
    {
        dataGridView1.CurrentRow.Cells[13].Style.BackColor = Color.GreenYellow;
    }
}                  

2 个答案:

答案 0 :(得分:0)

试试这个;

  private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        if (e.Value != null)
        {
            if (e.Value.ToString().Equals("X"))
            {
                e.CellStyle.BackColor = Color.Red;
            }
        }
    }

希望有所帮助,

答案 1 :(得分:0)

你必须这样做:

1-删除您的代码

for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
...
}

2-添加此活动

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        try
        {
            if ((e.RowIndex > -1 && e.ColumnIndex >-1))
            {
                if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString().Contains("    X"))
                        e.CellStyle.BackColor = Color.GreenYellow;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString());
        }
    }